File: Concatenating-Strings.html

package info (click to toggle)
octave 3.6.2-5%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 71,636 kB
  • sloc: cpp: 241,186; fortran: 23,651; sh: 14,790; ansic: 7,153; lex: 3,761; objc: 3,404; yacc: 3,386; makefile: 2,073; awk: 985; perl: 838
file content (249 lines) | stat: -rw-r--r-- 11,487 bytes parent folder | download
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
<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>