<html lang="en"> <head> <title>Concatenating Strings - Untitled</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Untitled"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Creating-Strings.html#Creating-Strings" title="Creating Strings"> <link rel="next" href="Conversion-of-Numerical-Data-to-Strings.html#Conversion-of-Numerical-Data-to-Strings" title="Conversion of Numerical Data to Strings"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Concatenating-Strings"></a> <p> Next: <a rel="next" accesskey="n" href="Conversion-of-Numerical-Data-to-Strings.html#Conversion-of-Numerical-Data-to-Strings">Conversion of Numerical Data to Strings</a>, Up: <a rel="up" accesskey="u" href="Creating-Strings.html#Creating-Strings">Creating Strings</a> <hr> </div> <h4 class="subsection">5.3.1 Concatenating Strings</h4> <p>It has been shown above that strings can be concatenated using matrix notation (see <a href="Strings.html#Strings">Strings</a>, <a href="Character-Arrays.html#Character-Arrays">Character Arrays</a>). Apart from that, there are several functions to concatenate string objects: <code>char</code>, <code>strvcat</code>, <code>strcat</code> and <code>cstrcat</code>. In addition, the general purpose concatenation functions can be used: see <a href="doc_002dcat.html#doc_002dcat">cat</a>, <a href="doc_002dhorzcat.html#doc_002dhorzcat">horzcat</a> and <a href="doc_002dvertcat.html#doc_002dvertcat">vertcat</a>. <ul> <li>All string concatenation functions except <code>cstrcat</code> convert numerical input into character data by taking the corresponding ASCII character for each element, as in the following example: <pre class="example"> char([98, 97, 110, 97, 110, 97]) ⇒ ans = banana </pre> <li><code>char</code> and <code>strvcat</code> concatenate vertically, while <code>strcat</code> and <code>cstrcat</code> concatenate horizontally. For example: <pre class="example"> char("an apple", "two pears") ⇒ ans = an apple two pears strcat("oc", "tave", " is", " good", " for you") ⇒ ans = octave is good for you </pre> <li><code>char</code> generates an empty row in the output for each empty string in the input. <code>strvcat</code>, on the other hand, eliminates empty strings. <pre class="example"> char("orange", "green", "", "red") ⇒ ans = orange green red strvcat("orange", "green", "", "red") ⇒ ans = orange green red </pre> <li>All string concatenation functions except <code>cstrcat</code> also accept cell array data (see <a href="Cell-Arrays.html#Cell-Arrays">Cell Arrays</a>). <code>char</code> and <code>strvcat</code> convert cell arrays into character arrays, while <code>strcat</code> concatenates within the cells of the cell arrays: <pre class="example"> char({"red", "green", "", "blue"}) ⇒ ans = red green blue strcat({"abc"; "ghi"}, {"def"; "jkl"}) ⇒ ans = { [1,1] = abcdef [2,1] = ghijkl } </pre> <li><code>strcat</code> removes trailing white space in the arguments (except within cell arrays), while <code>cstrcat</code> leaves white space untouched. Both kinds of behavior can be useful as can be seen in the examples: <pre class="example"> strcat(["dir1";"directory2"], ["/";"/"], ["file1";"file2"]) ⇒ ans = dir1/file1 directory2/file2 cstrcat(["thirteen apples"; "a banana"], [" 5$";" 1$"]) ⇒ ans = thirteen apples 5$ a banana 1$ </pre> <p>Note that in the above example for <code>cstrcat</code>, the white space originates from the internal representation of the strings in a string array (see <a href="Character-Arrays.html#Character-Arrays">Character Arrays</a>). </ul> <!-- strfns.cc --> <p><a name="doc_002dchar"></a> <div class="defun"> — Built-in Function: <b>char</b> (<var>x</var>)<var><a name="index-char-288"></a></var><br> — Built-in Function: <b>char</b> (<var>x, <small class="dots">...</small></var>)<var><a name="index-char-289"></a></var><br> — Built-in Function: <b>char</b> (<var>s1, s2, <small class="dots">...</small></var>)<var><a name="index-char-290"></a></var><br> — Built-in Function: <b>char</b> (<var>cell_array</var>)<var><a name="index-char-291"></a></var><br> <blockquote><p>Create a string array from one or more numeric matrices, character matrices, or cell arrays. 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>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>For cell arrays, each element is concatenated separately. Cell arrays converted through <code>char</code> can mostly be converted back with <code>cellstr</code>. For example, <pre class="example"> char ([97, 98, 99], "", {"98", "99", 100}, "str1", ["ha", "lf"]) ⇒ ["abc " " " "98 " "99 " "d " "str1 " "half "] </pre> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dstrvcat.html#doc_002dstrvcat">strvcat</a>, <a href="doc_002dcellstr.html#doc_002dcellstr">cellstr</a>. </p></blockquote></div> <!-- strfns.cc --> <p><a name="doc_002dstrvcat"></a> <div class="defun"> — Built-in Function: <b>strvcat</b> (<var>x</var>)<var><a name="index-strvcat-292"></a></var><br> — Built-in Function: <b>strvcat</b> (<var>x, <small class="dots">...</small></var>)<var><a name="index-strvcat-293"></a></var><br> — Built-in Function: <b>strvcat</b> (<var>s1, s2, <small class="dots">...</small></var>)<var><a name="index-strvcat-294"></a></var><br> — Built-in Function: <b>strvcat</b> (<var>cell_array</var>)<var><a name="index-strvcat-295"></a></var><br> <blockquote><p>Create a character array from one or more numeric matrices, character matrices, or cell arrays. 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>char</code>, empty strings are removed and will not appear in the output. <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>For cell arrays, each element is concatenated separately. Cell arrays converted through <code>strvcat</code> can mostly be converted back with <code>cellstr</code>. For example, <pre class="example"> strvcat ([97, 98, 99], "", {"98", "99", 100}, "str1", ["ha", "lf"]) ⇒ ["abc " "98 " "99 " "d " "str1 " "half "] </pre> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dchar.html#doc_002dchar">char</a>, <a href="doc_002dstrcat.html#doc_002dstrcat">strcat</a>, <a href="doc_002dcstrcat.html#doc_002dcstrcat">cstrcat</a>. </p></blockquote></div> <!-- ./strings/strcat.m --> <p><a name="doc_002dstrcat"></a> <div class="defun"> — Function File: <b>strcat</b> (<var>s1, s2, <small class="dots">...</small></var>)<var><a name="index-strcat-296"></a></var><br> <blockquote><p>Return a string containing all the arguments concatenated horizontally. If the arguments are cells strings, <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 is eliminated. For example, <pre class="example"> s = [ "ab"; "cde" ]; strcat (s, s, s) ⇒ ans = "ab ab ab " "cdecdecde" </pre> <pre class="example"> s = { "ab"; "cde" }; strcat (s, s, s) ⇒ ans = { [1,1] = ababab [2,1] = cdecdecde } </pre> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dcstrcat.html#doc_002dcstrcat">cstrcat</a>, <a href="doc_002dchar.html#doc_002dchar">char</a>, <a href="doc_002dstrvcat.html#doc_002dstrvcat">strvcat</a>. </p></blockquote></div> <!-- ./strings/cstrcat.m --> <p><a name="doc_002dcstrcat"></a> <div class="defun"> — Function File: <b>cstrcat</b> (<var>s1, s2, <small class="dots">...</small></var>)<var><a name="index-cstrcat-297"></a></var><br> <blockquote><p>Return a string containing all the arguments concatenated horizontally. Trailing white space is preserved. For example, <pre class="example"> cstrcat ("ab ", "cd") ⇒ "ab cd" </pre> <pre class="example"> s = [ "ab"; "cde" ]; cstrcat (s, s, s) ⇒ ans = "ab ab ab " "cdecdecde" </pre> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dstrcat.html#doc_002dstrcat">strcat</a>, <a href="doc_002dchar.html#doc_002dchar">char</a>, <a href="doc_002dstrvcat.html#doc_002dstrvcat">strvcat</a>. </p></blockquote></div> </body></html>