Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > b38d2da330d1936e5ab1307c039c4941 > files > 174

octave-doc-3.6.4-3.mga4.noarch.rpm

<html lang="en">
<head>
<title>Concatenating Strings - GNU Octave</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="GNU Octave">
<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:&nbsp;<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:&nbsp;<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])
               &rArr; 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")
               &rArr; ans =
                 an apple
                 two pears
          
          strcat("oc", "tave", " is", " good", " for you")
               &rArr; 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")
               &rArr; ans =
                 orange
                 green
          
                 red
          
          strvcat("orange", "green", "", "red")
               &rArr; 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"})
               &rArr; ans =
                 red
                 green
          
                 blue
          
          strcat({"abc"; "ghi"}, {"def"; "jkl"})
               &rArr; 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"])
               &rArr; ans =
                 dir1/file1
                 directory2/file2
          
          cstrcat(["thirteen apples"; "a banana"], [" 5$";" 1$"])
               &rArr; 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>

<!-- char src/strfns.cc -->
   <p><a name="doc_002dchar"></a>

<div class="defun">
&mdash; Built-in Function:  <b>char</b> (<var>x</var>)<var><a name="index-char-333"></a></var><br>
&mdash; Built-in Function:  <b>char</b> (<var>x, <small class="dots">...</small></var>)<var><a name="index-char-334"></a></var><br>
&mdash; Built-in Function:  <b>char</b> (<var>s1, s2, <small class="dots">...</small></var>)<var><a name="index-char-335"></a></var><br>
&mdash; Built-in Function:  <b>char</b> (<var>cell_array</var>)<var><a name="index-char-336"></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"])
               &rArr; ["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>

<!-- strvcat src/strfns.cc -->
   <p><a name="doc_002dstrvcat"></a>

<div class="defun">
&mdash; Built-in Function:  <b>strvcat</b> (<var>x</var>)<var><a name="index-strvcat-337"></a></var><br>
&mdash; Built-in Function:  <b>strvcat</b> (<var>x, <small class="dots">...</small></var>)<var><a name="index-strvcat-338"></a></var><br>
&mdash; Built-in Function:  <b>strvcat</b> (<var>s1, s2, <small class="dots">...</small></var>)<var><a name="index-strvcat-339"></a></var><br>
&mdash; Built-in Function:  <b>strvcat</b> (<var>cell_array</var>)<var><a name="index-strvcat-340"></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"])
               &rArr; ["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>

<!-- strcat scripts/strings/strcat.m -->
   <p><a name="doc_002dstrcat"></a>

<div class="defun">
&mdash; Function File:  <b>strcat</b> (<var>s1, s2, <small class="dots">...</small></var>)<var><a name="index-strcat-341"></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)
              &rArr;
                  "ab ab ab "
                  "cdecdecde"
</pre>
        <pre class="example">          s = { "ab"; "cde" };
          strcat (s, s, s)
              &rArr;
                  {
                    [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>

<!-- cstrcat scripts/strings/cstrcat.m -->
   <p><a name="doc_002dcstrcat"></a>

<div class="defun">
&mdash; Function File:  <b>cstrcat</b> (<var>s1, s2, <small class="dots">...</small></var>)<var><a name="index-cstrcat-342"></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")
                &rArr; "ab   cd"
</pre>
        <pre class="example">          s = [ "ab"; "cde" ];
          cstrcat (s, s, s)
               &rArr; "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>