1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Concatenating Strings (GNU Octave (version 10.3.0))</title>
<meta name="description" content="Concatenating Strings (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Concatenating Strings (GNU Octave (version 10.3.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="String-Operations.html" rel="up" title="String Operations">
<link href="Splitting-and-Joining-Strings.html" rel="next" title="Splitting and Joining Strings">
<link href="Common-String-Operations.html" rel="prev" title="Common String Operations">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
ul.mark-bullet {list-style-type: disc}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<div class="subsection-level-extent" id="Concatenating-Strings">
<div class="nav-panel">
<p>
Next: <a href="Splitting-and-Joining-Strings.html" accesskey="n" rel="next">Splitting and Joining Strings</a>, Previous: <a href="Common-String-Operations.html" accesskey="p" rel="prev">Common String Operations</a>, Up: <a href="String-Operations.html" accesskey="u" rel="up">String Operations</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="Concatenating-Strings-1"><span>5.3.2 Concatenating Strings<a class="copiable-link" href="#Concatenating-Strings-1"> ¶</a></span></h4>
<p>Strings can be concatenated using matrix notation
(see <a class="pxref" href="Strings.html">Strings</a>, <a class="ref" href="Character-Arrays.html">Character Arrays</a>) which is often the most natural
method. For example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">fullname = [fname ".txt"];
email = ["<" user "@" domain ">"];
</pre></div></div>
<p>In each case it is easy to see what the final string will look like. This
method is also the most efficient. When using matrix concatenation the parser
immediately begins joining the strings without having to process
the overhead of a function call and the input validation of the associated
function.
</p>
<p>The <code class="code">newline</code> function can be used to join strings such that they appear
as multiple lines of text when displayed.
</p>
<a class="anchor" id="XREFnewline"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-newline"><span><code class="def-type"><var class="var">c</var> =</code> <strong class="def-name">newline</strong><a class="copiable-link" href="#index-newline"> ¶</a></span></dt>
<dd><p>Return the character corresponding to a newline.
</p>
<p>This is equivalent to <code class="code">"\n"</code>.
</p>
<p>Example Code
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">joined_string = [newline "line1" newline "line2"]
⇒
line1
line2
</pre></div></div>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFstrcat">strcat</a>, <a class="ref" href="Splitting-and-Joining-Strings.html#XREFstrjoin">strjoin</a>, <a class="ref" href="Splitting-and-Joining-Strings.html#XREFstrsplit">strsplit</a>.
</p></dd></dl>
<p>In addition, there are several other functions for concatenating string
objects which can be useful in specific circumstances: <code class="code">char</code>,
<code class="code">strvcat</code>, <code class="code">strcat</code>, and <code class="code">cstrcat</code>. Finally, the general
purpose concatenation functions can be used: see <a class="ref" href="Rearranging-Matrices.html#XREFcat">cat</a>,
<a class="ref" href="Rearranging-Matrices.html#XREFhorzcat">horzcat</a>, and <a class="ref" href="Rearranging-Matrices.html#XREFvertcat">vertcat</a>.
</p>
<ul class="itemize mark-bullet">
<li>All string concatenation functions except <code class="code">cstrcat</code>
convert numerical input into character data by taking the corresponding UTF-8
character for each element (or multi-byte sequence), as in the following
example:
<div class="example">
<div class="group"><pre class="example-preformatted">char ([98, 97, 110, 97, 110, 97])
⇒ banana
</pre></div></div>
<p>For conversion between locale encodings and UTF-8, see
<a class="ref" href="String-encoding.html#XREFunicode2native">unicode2native</a> and
<a class="ref" href="String-encoding.html#XREFnative2unicode">native2unicode</a>.
</p>
</li><li><code class="code">char</code> and <code class="code">strvcat</code>
concatenate vertically, while <code class="code">strcat</code> and <code class="code">cstrcat</code> concatenate
horizontally. For example:
<div class="example">
<div class="group"><pre class="example-preformatted">char ("an apple", "two pears")
⇒ an apple
two pears
</pre></div><pre class="example-preformatted">
</pre><div class="group"><pre class="example-preformatted">strcat ("oc", "tave", " is", " good", " for you")
⇒ octave is good for you
</pre></div></div>
</li><li><code class="code">char</code> generates an empty row in the output
for each empty string in the input. <code class="code">strvcat</code>, on the other hand,
eliminates empty strings.
<div class="example">
<div class="group"><pre class="example-preformatted">char ("orange", "green", "", "red")
⇒ orange
green
red
</pre></div><pre class="example-preformatted">
</pre><div class="group"><pre class="example-preformatted">strvcat ("orange", "green", "", "red")
⇒ orange
green
red
</pre></div></div>
</li><li>All string concatenation functions except <code class="code">cstrcat</code> also accept cell
array data (see <a class="pxref" href="Cell-Arrays.html">Cell Arrays</a>). <code class="code">char</code> and
<code class="code">strvcat</code> convert cell arrays into character arrays, while <code class="code">strcat</code>
concatenates within the cells of the cell arrays:
<div class="example">
<div class="group"><pre class="example-preformatted">char ({"red", "green", "", "blue"})
⇒ red
green
blue
</pre></div><pre class="example-preformatted">
</pre><div class="group"><pre class="example-preformatted">strcat ({"abc"; "ghi"}, {"def"; "jkl"})
⇒
{
[1,1] = abcdef
[2,1] = ghijkl
}
</pre></div></div>
</li><li><code class="code">strcat</code> removes trailing white space in the arguments (except
within cell arrays), while <code class="code">cstrcat</code> leaves white space untouched. Both
kinds of behavior can be useful as can be seen in the examples:
<div class="example">
<div class="group"><pre class="example-preformatted">strcat (["dir1";"directory2"], ["/";"/"], ["file1";"file2"])
⇒ dir1/file1
directory2/file2
</pre></div><div class="group"><pre class="example-preformatted">
cstrcat (["thirteen apples"; "a banana"], [" 5$";" 1$"])
⇒ thirteen apples 5$
a banana 1$
</pre></div></div>
<p>Note that in the above example for <code class="code">cstrcat</code>, the white space originates
from the internal representation of the strings in a string array
(see <a class="pxref" href="Character-Arrays.html">Character Arrays</a>).
</p></li></ul>
<a class="anchor" id="XREFchar"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-char"><span><code class="def-type"><var class="var">C</var> =</code> <strong class="def-name">char</strong> <code class="def-code-arguments">(<var class="var">A</var>)</code><a class="copiable-link" href="#index-char"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-char-1"><span><code class="def-type"><var class="var">C</var> =</code> <strong class="def-name">char</strong> <code class="def-code-arguments">(<var class="var">A</var>, …)</code><a class="copiable-link" href="#index-char-1"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-char-2"><span><code class="def-type"><var class="var">C</var> =</code> <strong class="def-name">char</strong> <code class="def-code-arguments">(<var class="var">str1</var>, <var class="var">str2</var>, …)</code><a class="copiable-link" href="#index-char-2"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-char-3"><span><code class="def-type"><var class="var">C</var> =</code> <strong class="def-name">char</strong> <code class="def-code-arguments">(<var class="var">cell_array</var>)</code><a class="copiable-link" href="#index-char-3"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-char-4"><span><code class="def-type">'' =</code> <strong class="def-name">char</strong> <code class="def-code-arguments">()</code><a class="copiable-link" href="#index-char-4"> ¶</a></span></dt>
<dd>
<p>Create a string array from one or more numeric matrices, character
matrices, or cell arrays.
</p>
<p>Arguments are concatenated vertically. The returned values are padded with
blanks as needed to make each row of the string array have the same length.
Empty input strings are significant and will concatenated in the output.
</p>
<p>For numerical input, each element is converted to the corresponding ASCII
character. A range error results if an input is outside the ASCII range
(0-255).
</p>
<p>For cell arrays, each element is concatenated separately. Cell arrays
converted through <code class="code">char</code> can mostly be converted back with
<code class="code">cellstr</code>. For example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">char ([97, 98, 99], "", {"98", "99", 100}, "str1", ["ha", "lf"])
⇒ ["abc "
" "
"98 "
"99 "
"d "
"str1"
"half"]
</pre></div></div>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFstrvcat">strvcat</a>, <a class="ref" href="Cell-Arrays-of-Strings.html#XREFcellstr">cellstr</a>.
</p></dd></dl>
<a class="anchor" id="XREFstrvcat"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-strvcat"><span><code class="def-type"><var class="var">C</var> =</code> <strong class="def-name">strvcat</strong> <code class="def-code-arguments">(<var class="var">A</var>)</code><a class="copiable-link" href="#index-strvcat"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-strvcat-1"><span><code class="def-type"><var class="var">C</var> =</code> <strong class="def-name">strvcat</strong> <code class="def-code-arguments">(<var class="var">A</var>, …)</code><a class="copiable-link" href="#index-strvcat-1"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-strvcat-2"><span><code class="def-type"><var class="var">C</var> =</code> <strong class="def-name">strvcat</strong> <code class="def-code-arguments">(<var class="var">str1</var>, <var class="var">str2</var>, …)</code><a class="copiable-link" href="#index-strvcat-2"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-strvcat-3"><span><code class="def-type"><var class="var">C</var> =</code> <strong class="def-name">strvcat</strong> <code class="def-code-arguments">(<var class="var">cell_array</var>)</code><a class="copiable-link" href="#index-strvcat-3"> ¶</a></span></dt>
<dd><p>Create a character array from one or more numeric matrices, character
matrices, or cell arrays.
</p>
<p>Arguments are concatenated vertically. The returned values are padded with
blanks as needed to make each row of the string array have the same length.
Unlike <code class="code">char</code>, empty strings are removed and will not appear in the
output.
</p>
<p>For numerical input, each element is converted to the corresponding ASCII
character. A range error results if an input is outside the ASCII range
(0-255).
</p>
<p>For cell arrays, each element is concatenated separately. Cell arrays
converted through <code class="code">strvcat</code> can mostly be converted back with
<code class="code">cellstr</code>. For example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">strvcat ([97, 98, 99], "", {"98", "99", 100}, "str1", ["ha", "lf"])
⇒ ["abc "
"98 "
"99 "
"d "
"str1"
"half"]
</pre></div></div>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFchar">char</a>, <a class="ref" href="#XREFstrcat">strcat</a>, <a class="ref" href="#XREFcstrcat">cstrcat</a>.
</p></dd></dl>
<a class="anchor" id="XREFstrcat"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-strcat"><span><code class="def-type"><var class="var">str</var> =</code> <strong class="def-name">strcat</strong> <code class="def-code-arguments">(<var class="var">s1</var>, <var class="var">s2</var>, …)</code><a class="copiable-link" href="#index-strcat"> ¶</a></span></dt>
<dd><p>Return a string containing all the arguments concatenated horizontally.
</p>
<p>If the arguments are cell strings, <code class="code">strcat</code> returns a cell string
with the individual cells concatenated. For numerical input, each element
is converted to the corresponding ASCII character. Trailing white space
for any character string input is eliminated before the strings are
concatenated. Note that cell string values do <strong class="strong">not</strong> have
whitespace trimmed.
</p>
<p>For example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">strcat ("|", " leading space is preserved", "|")
⇒ | leading space is preserved|
</pre></div></div>
<div class="example">
<div class="group"><pre class="example-preformatted">strcat ("|", "trailing space is eliminated ", "|")
⇒ |trailing space is eliminated|
</pre></div></div>
<div class="example">
<div class="group"><pre class="example-preformatted">strcat ("homogeneous space |", " ", "| is also eliminated")
⇒ homogeneous space || is also eliminated
</pre></div></div>
<div class="example">
<div class="group"><pre class="example-preformatted">s = [ "ab"; "cde" ];
strcat (s, s, s)
⇒
"ababab "
"cdecdecde"
</pre></div></div>
<div class="example">
<div class="group"><pre class="example-preformatted">s = { "ab"; "cd " };
strcat (s, s, s)
⇒
{
[1,1] = ababab
[2,1] = cd cd cd
}
</pre></div></div>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFcstrcat">cstrcat</a>, <a class="ref" href="#XREFchar">char</a>, <a class="ref" href="#XREFstrvcat">strvcat</a>.
</p></dd></dl>
<a class="anchor" id="XREFcstrcat"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-cstrcat"><span><code class="def-type"><var class="var">str</var> =</code> <strong class="def-name">cstrcat</strong> <code class="def-code-arguments">(<var class="var">s1</var>, <var class="var">s2</var>, …)</code><a class="copiable-link" href="#index-cstrcat"> ¶</a></span></dt>
<dd><p>Return a string containing all the arguments concatenated horizontally
with trailing white space preserved.
</p>
<p>For example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">cstrcat ("ab ", "cd")
⇒ "ab cd"
</pre></div></div>
<div class="example">
<div class="group"><pre class="example-preformatted">s = [ "ab"; "cde" ];
cstrcat (s, s, s)
⇒ "ab ab ab "
"cdecdecde"
</pre></div></div>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFstrcat">strcat</a>, <a class="ref" href="#XREFchar">char</a>, <a class="ref" href="#XREFstrvcat">strvcat</a>.
</p></dd></dl>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Splitting-and-Joining-Strings.html">Splitting and Joining Strings</a>, Previous: <a href="Common-String-Operations.html">Common String Operations</a>, Up: <a href="String-Operations.html">String Operations</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|