~gabriel1984sibiu/octave/octave

« back to all changes in this revision

Viewing changes to doc/interpreter/strings.txi

  • Committer: Grevutiu Gabriel
  • Date: 2014-01-02 13:05:54 UTC
  • Revision ID: gabriel1984sibiu@gmail.com-20140102130554-3r7ivdjln1ni6kcg
New version (3.8.0) from upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
@c Copyright (C) 1996-2013 John W. Eaton
 
2
@c
 
3
@c This file is part of Octave.
 
4
@c
 
5
@c Octave is free software; you can redistribute it and/or modify it
 
6
@c under the terms of the GNU General Public License as published by the
 
7
@c Free Software Foundation; either version 3 of the License, or (at
 
8
@c your option) any later version.
 
9
@c 
 
10
@c Octave is distributed in the hope that it will be useful, but WITHOUT
 
11
@c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
12
@c FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
13
@c for more details.
 
14
@c 
 
15
@c You should have received a copy of the GNU General Public License
 
16
@c along with Octave; see the file COPYING.  If not, see
 
17
@c <http://www.gnu.org/licenses/>.
 
18
 
 
19
@node Strings
 
20
@chapter Strings
 
21
@cindex strings
 
22
@cindex character strings
 
23
@opindex "
 
24
@opindex '
 
25
 
 
26
A @dfn{string constant} consists of a sequence of characters enclosed in
 
27
either double-quote or single-quote marks.  For example, both of the
 
28
following expressions
 
29
 
 
30
@example
 
31
@group
 
32
"parrot"
 
33
'parrot'
 
34
@end group
 
35
@end example
 
36
 
 
37
@noindent
 
38
represent the string whose contents are @samp{parrot}.  Strings in
 
39
Octave can be of any length.
 
40
 
 
41
Since the single-quote mark is also used for the transpose operator
 
42
(@pxref{Arithmetic Ops}) but double-quote marks have no other purpose in Octave,
 
43
it is best to use double-quote marks to denote strings.
 
44
 
 
45
Strings can be concatenated using the notation for defining matrices.  For
 
46
example, the expression 
 
47
 
 
48
@example
 
49
[ "foo" , "bar" , "baz" ]
 
50
@end example
 
51
 
 
52
@noindent
 
53
produces the string whose contents are @samp{foobarbaz}.  @xref{Numeric Data
 
54
Types}, for more information about creating matrices.
 
55
 
 
56
@menu
 
57
* Escape Sequences in String Constants::
 
58
* Character Arrays::
 
59
* Creating Strings::
 
60
* Comparing Strings::
 
61
* Manipulating Strings::
 
62
* String Conversions::
 
63
* Character Class Functions::
 
64
@end menu
 
65
 
 
66
@node Escape Sequences in String Constants
 
67
@section Escape Sequences in String Constants
 
68
@cindex escape sequence notation
 
69
In double-quoted strings, the backslash character is used to introduce
 
70
@dfn{escape sequences} that represent other characters.  For example,
 
71
@samp{\n} embeds a newline character in a double-quoted string and
 
72
@samp{\"} embeds a double quote character.  In single-quoted strings, backslash
 
73
is not a special character.  Here is an example showing the difference:
 
74
 
 
75
@example
 
76
@group
 
77
toascii ("\n")
 
78
    @result{} 10
 
79
toascii ('\n')
 
80
    @result{} [ 92 110 ]
 
81
@end group
 
82
@end example
 
83
 
 
84
Here is a table of all the escape sequences used in Octave (within
 
85
double quoted strings).  They are the same as those used in the C 
 
86
programming language.
 
87
 
 
88
@table @code
 
89
@item \\
 
90
Represents a literal backslash, @samp{\}.
 
91
 
 
92
@item \"
 
93
Represents a literal double-quote character, @samp{"}.
 
94
 
 
95
@item \'
 
96
Represents a literal single-quote character, @samp{'}.
 
97
 
 
98
@item \0
 
99
Represents the null character, control-@@, ASCII code 0.
 
100
 
 
101
@item \a
 
102
Represents the ``alert'' character, control-g, ASCII code 7.
 
103
 
 
104
@item \b
 
105
Represents a backspace, control-h, ASCII code 8.
 
106
 
 
107
@item \f
 
108
Represents a formfeed, control-l, ASCII code 12.
 
109
 
 
110
@item \n
 
111
Represents a newline, control-j, ASCII code 10.
 
112
 
 
113
@item \r
 
114
Represents a carriage return, control-m, ASCII code 13.
 
115
 
 
116
@item \t
 
117
Represents a horizontal tab, control-i, ASCII code 9.
 
118
 
 
119
@item \v
 
120
Represents a vertical tab, control-k, ASCII code 11.
 
121
 
 
122
@item \@var{nnn}
 
123
Represents the octal value @var{nnn}, where @var{nnn} are one to three
 
124
digits between 0 and 7.  For example, the code for the ASCII ESC
 
125
(escape) character is @samp{\033}.
 
126
 
 
127
@item \x@var{hh}@dots{}
 
128
Represents the hexadecimal value @var{hh}, where @var{hh} are hexadecimal
 
129
digits (@samp{0} through @samp{9} and either @samp{A} through @samp{F} or
 
130
@samp{a} through @samp{f}).  Like the same construct in @sc{ansi} C,
 
131
the escape sequence continues until the first non-hexadecimal digit is seen.
 
132
However, using more than two hexadecimal digits produces undefined results.
 
133
@end table
 
134
 
 
135
In a single-quoted string there is only one escape sequence: you may insert a
 
136
single quote character using two single quote characters in succession.  For
 
137
example,
 
138
 
 
139
@example
 
140
@group
 
141
'I can''t escape'
 
142
    @result{} I can't escape
 
143
@end group
 
144
@end example
 
145
 
 
146
In scripts the two different string types can be distinguished if necessary
 
147
by using @code{is_dq_string} and @code{is_sq_string}.
 
148
 
 
149
@DOCSTRING(is_dq_string)
 
150
 
 
151
@DOCSTRING(is_sq_string)
 
152
 
 
153
@node Character Arrays
 
154
@section Character Arrays
 
155
 
 
156
The string representation used by Octave is an array of characters, so
 
157
internally the string @nospell{@qcode{"dddddddddd"}} is actually a row vector
 
158
of length 10 containing the value 100 in all places (100 is the ASCII code of
 
159
@qcode{"d"}).  This lends itself to the obvious generalization to character
 
160
matrices.  Using a matrix of characters, it is possible to represent a
 
161
collection of same-length strings in one variable.  The convention used in
 
162
Octave is that each row in a character matrix is a separate string, but letting
 
163
each column represent a string is equally possible.
 
164
 
 
165
The easiest way to create a character matrix is to put several strings
 
166
together into a matrix.
 
167
 
 
168
@example
 
169
collection = [ "String #1"; "String #2" ];
 
170
@end example
 
171
 
 
172
@noindent
 
173
This creates a 2-by-9 character matrix.
 
174
 
 
175
The function @code{ischar} can be used to test if an object is a character
 
176
matrix.
 
177
 
 
178
@DOCSTRING(ischar)
 
179
 
 
180
To test if an object is a string (i.e., a character vector and not a character
 
181
matrix) you can use the @code{ischar} function in combination with the
 
182
@code{isvector} function as in the following example:
 
183
 
 
184
@example
 
185
@group
 
186
ischar (collection)
 
187
     @result{} 1
 
188
 
 
189
ischar (collection) && isvector (collection)
 
190
     @result{} 0
 
191
 
 
192
ischar ("my string") && isvector ("my string")
 
193
     @result{} 1
 
194
@end group
 
195
@end example
 
196
 
 
197
One relevant question is, what happens when a character matrix is
 
198
created from strings of different length.  The answer is that Octave
 
199
puts blank characters at the end of strings shorter than the longest
 
200
string.  It is possible to use a different character than the
 
201
blank character using the @code{string_fill_char} function.
 
202
 
 
203
@DOCSTRING(string_fill_char)
 
204
 
 
205
This shows a problem with character matrices.  It simply isn't possible to
 
206
represent strings of different lengths.  The solution is to use a cell array of
 
207
strings, which is described in @ref{Cell Arrays of Strings}.
 
208
 
 
209
@node Creating Strings
 
210
@section Creating Strings
 
211
 
 
212
The easiest way to create a string is, as illustrated in the introduction,
 
213
to enclose a text in double-quotes or single-quotes.  It is however
 
214
possible to create a string without actually writing a text.  The
 
215
function @code{blanks} creates a string of a given length consisting
 
216
only of blank characters (ASCII code 32).
 
217
 
 
218
@DOCSTRING(blanks)
 
219
 
 
220
@menu
 
221
* Concatenating Strings::
 
222
* Converting Numerical Data to Strings::
 
223
@end menu
 
224
 
 
225
@node Concatenating Strings
 
226
@subsection Concatenating Strings
 
227
 
 
228
Strings can be concatenated using matrix notation
 
229
(@pxref{Strings}, @ref{Character Arrays}) which is often the most natural
 
230
method.  For example:
 
231
 
 
232
@example
 
233
@group
 
234
fullname = [fname ".txt"];
 
235
email = ["<" user "@@" domain ">"];
 
236
@end group
 
237
@end example
 
238
 
 
239
@noindent
 
240
In each case it is easy to see what the final string will look like.  This
 
241
method is also the most efficient.  When using matrix concatenation the parser
 
242
immediately begins joining the strings without having to process
 
243
the overhead of a function call and the input validation of the associated
 
244
function.
 
245
 
 
246
Nevertheless, there are several other functions for concatenating string
 
247
objects which can be useful in specific circumstances: @code{char},
 
248
@code{strvcat}, @code{strcat}, and @code{cstrcat}.  Finally, the general
 
249
purpose concatenation functions can be used: see @ref{XREFcat,,cat},
 
250
@ref{XREFhorzcat,,horzcat}, and @ref{XREFvertcat,,vertcat}.
 
251
 
 
252
@itemize @bullet
 
253
@item All string concatenation functions except @code{cstrcat}
 
254
convert numerical input into character data by taking the corresponding ASCII
 
255
character for each element, as in the following example:
 
256
 
 
257
@example
 
258
@group
 
259
char ([98, 97, 110, 97, 110, 97])
 
260
   @result{} banana
 
261
@end group
 
262
@end example
 
263
 
 
264
@item
 
265
@code{char} and @code{strvcat}
 
266
concatenate vertically, while @code{strcat} and @code{cstrcat} concatenate
 
267
horizontally.  For example:
 
268
 
 
269
@example
 
270
@group
 
271
char ("an apple", "two pears")
 
272
    @result{} an apple
 
273
       two pears
 
274
@end group
 
275
 
 
276
@group
 
277
strcat ("oc", "tave", " is", " good", " for you")
 
278
     @result{} octave is good for you
 
279
@end group
 
280
@end example
 
281
 
 
282
@item @code{char} generates an empty row in the output
 
283
for each empty string in the input.  @code{strvcat}, on the other hand,
 
284
eliminates empty strings.
 
285
 
 
286
@example
 
287
@group
 
288
char ("orange", "green", "", "red")
 
289
    @result{} orange
 
290
       green 
 
291
 
 
292
       red
 
293
@end group
 
294
 
 
295
@group
 
296
strvcat ("orange", "green", "", "red")
 
297
    @result{} orange
 
298
       green 
 
299
       red  
 
300
@end group
 
301
@end example
 
302
 
 
303
@item All string concatenation functions except @code{cstrcat} also accept cell
 
304
array data (@pxref{Cell Arrays}).  @code{char} and
 
305
@code{strvcat} convert cell arrays into character arrays, while @code{strcat}
 
306
concatenates within the cells of the cell arrays:
 
307
 
 
308
@example
 
309
@group
 
310
char (@{"red", "green", "", "blue"@})
 
311
     @result{} red  
 
312
        green
 
313
 
 
314
        blue 
 
315
@end group
 
316
 
 
317
@group
 
318
strcat (@{"abc"; "ghi"@}, @{"def"; "jkl"@})
 
319
     @result{}
 
320
        @{
 
321
          [1,1] = abcdef
 
322
          [2,1] = ghijkl
 
323
        @}
 
324
@end group
 
325
@end example
 
326
 
 
327
@item @code{strcat} removes trailing white space in the arguments (except
 
328
within cell arrays), while @code{cstrcat} leaves white space untouched.  Both
 
329
kinds of behavior can be useful as can be seen in the examples:
 
330
 
 
331
@example
 
332
@group
 
333
strcat (["dir1";"directory2"], ["/";"/"], ["file1";"file2"])
 
334
     @result{} dir1/file1
 
335
        directory2/file2
 
336
@end group
 
337
@group
 
338
 
 
339
cstrcat (["thirteen apples"; "a banana"], [" 5$";" 1$"])
 
340
      @result{} thirteen apples 5$
 
341
         a banana        1$
 
342
@end group
 
343
@end example
 
344
 
 
345
Note that in the above example for @code{cstrcat}, the white space originates
 
346
from the internal representation of the strings in a string array
 
347
(@pxref{Character Arrays}).
 
348
@end itemize
 
349
 
 
350
@DOCSTRING(char)
 
351
 
 
352
@DOCSTRING(strvcat)
 
353
 
 
354
@DOCSTRING(strcat)
 
355
 
 
356
@DOCSTRING(cstrcat)
 
357
 
 
358
@node Converting Numerical Data to Strings
 
359
@subsection Converting Numerical Data to Strings
 
360
Apart from the string concatenation functions (@pxref{Concatenating Strings})
 
361
which cast numerical data to the corresponding ASCII characters, there are
 
362
several functions that format numerical data as strings.  @code{mat2str} and
 
363
@code{num2str} convert real or complex matrices, while @code{int2str} converts
 
364
integer matrices.  @code{int2str} takes the real part of complex values and
 
365
round fractional values to integer.  A more flexible way to format numerical
 
366
data as strings is the @code{sprintf} function (@pxref{Formatted Output},
 
367
@ref{XREFsprintf,,sprintf}).
 
368
 
 
369
@DOCSTRING(mat2str)
 
370
 
 
371
@DOCSTRING(num2str)
 
372
 
 
373
@DOCSTRING(int2str)
 
374
 
 
375
@node Comparing Strings
 
376
@section Comparing Strings
 
377
 
 
378
Since a string is a character array, comparisons between strings work
 
379
element by element as the following example shows:
 
380
 
 
381
@example
 
382
@group
 
383
GNU = "GNU's Not UNIX";
 
384
spaces = (GNU == " ")
 
385
     @result{} spaces =
 
386
       0   0   0   0   0   1   0   0   0   1   0   0   0   0
 
387
@end group
 
388
@end example
 
389
 
 
390
@noindent To determine if two strings are identical it is necessary to use the
 
391
@code{strcmp} function.  It compares complete strings and is case
 
392
sensitive.  @code{strncmp} compares only the first @code{N} characters (with
 
393
@code{N} given as a parameter).  @code{strcmpi} and @code{strncmpi} are the
 
394
corresponding functions for case-insensitive comparison.
 
395
 
 
396
@DOCSTRING(strcmp)
 
397
 
 
398
@DOCSTRING(strncmp)
 
399
 
 
400
@DOCSTRING(strcmpi)
 
401
 
 
402
@DOCSTRING(strncmpi)
 
403
 
 
404
@DOCSTRING(validatestring)
 
405
 
 
406
@node Manipulating Strings
 
407
@section Manipulating Strings
 
408
 
 
409
Octave supports a wide range of functions for manipulating strings.
 
410
Since a string is just a matrix, simple manipulations can be accomplished
 
411
using standard operators.  The following example shows how to replace
 
412
all blank characters with underscores.
 
413
 
 
414
@example
 
415
@group
 
416
quote = ...
 
417
  "First things first, but not necessarily in that order";
 
418
quote( quote == " " ) = "_"
 
419
@result{} quote = 
 
420
    First_things_first,_but_not_necessarily_in_that_order
 
421
@end group
 
422
@end example
 
423
 
 
424
For more complex manipulations, such as searching, replacing, and
 
425
general regular expressions, the following functions come with Octave.
 
426
 
 
427
@DOCSTRING(deblank)
 
428
 
 
429
@DOCSTRING(strtrim)
 
430
 
 
431
@DOCSTRING(strtrunc)
 
432
 
 
433
@DOCSTRING(findstr)
 
434
 
 
435
@DOCSTRING(strchr)
 
436
 
 
437
@DOCSTRING(index)
 
438
 
 
439
@DOCSTRING(rindex)
 
440
 
 
441
@DOCSTRING(strfind)
 
442
 
 
443
@DOCSTRING(strjoin)
 
444
 
 
445
@DOCSTRING(strmatch)
 
446
 
 
447
@DOCSTRING(strtok)
 
448
 
 
449
@DOCSTRING(strsplit)
 
450
 
 
451
@DOCSTRING(ostrsplit)
 
452
 
 
453
@DOCSTRING(strread)
 
454
 
 
455
@DOCSTRING(strrep)
 
456
 
 
457
@DOCSTRING(substr)
 
458
 
 
459
@DOCSTRING(regexp)
 
460
 
 
461
@DOCSTRING(regexpi)
 
462
 
 
463
@DOCSTRING(regexprep)
 
464
 
 
465
@DOCSTRING(regexptranslate)
 
466
 
 
467
@DOCSTRING(untabify)
 
468
 
 
469
@node String Conversions
 
470
@section String Conversions
 
471
 
 
472
Octave supports various kinds of conversions between strings and
 
473
numbers.  As an example, it is possible to convert a string containing
 
474
a hexadecimal number to a floating point number.
 
475
 
 
476
@example
 
477
@group
 
478
hex2dec ("FF")
 
479
      @result{} 255
 
480
@end group
 
481
@end example
 
482
 
 
483
@DOCSTRING(bin2dec)
 
484
 
 
485
@DOCSTRING(dec2bin)
 
486
 
 
487
@DOCSTRING(dec2hex)
 
488
 
 
489
@DOCSTRING(hex2dec)
 
490
 
 
491
@DOCSTRING(dec2base)
 
492
 
 
493
@DOCSTRING(base2dec)
 
494
 
 
495
@DOCSTRING(num2hex)
 
496
 
 
497
@DOCSTRING(hex2num)
 
498
 
 
499
@DOCSTRING(str2double)
 
500
 
 
501
@DOCSTRING(strjust)
 
502
 
 
503
@DOCSTRING(str2num)
 
504
 
 
505
@DOCSTRING(toascii)
 
506
 
 
507
@DOCSTRING(tolower)
 
508
 
 
509
@DOCSTRING(toupper)
 
510
 
 
511
@DOCSTRING(do_string_escapes)
 
512
 
 
513
@DOCSTRING(undo_string_escapes)
 
514
 
 
515
@node Character Class Functions
 
516
@section Character Class Functions
 
517
 
 
518
Octave also provides the following character class test functions
 
519
patterned after the functions in the standard C library.  They all
 
520
operate on string arrays and return matrices of zeros and ones.
 
521
Elements that are nonzero indicate that the condition was true for the
 
522
corresponding character in the string array.  For example:
 
523
 
 
524
@example
 
525
@group
 
526
isalpha ("!Q@@WERT^Y&")
 
527
     @result{} [ 0, 1, 0, 1, 1, 1, 1, 0, 1, 0 ]
 
528
@end group
 
529
@end example
 
530
 
 
531
@DOCSTRING(isalnum)
 
532
 
 
533
@DOCSTRING(isalpha)
 
534
 
 
535
@DOCSTRING(isletter)
 
536
 
 
537
@DOCSTRING(islower)
 
538
 
 
539
@DOCSTRING(isupper)
 
540
 
 
541
@DOCSTRING(isdigit)
 
542
 
 
543
@DOCSTRING(isxdigit)
 
544
 
 
545
@DOCSTRING(ispunct)
 
546
 
 
547
@DOCSTRING(isspace)
 
548
 
 
549
@DOCSTRING(iscntrl)
 
550
 
 
551
@DOCSTRING(isgraph)
 
552
 
 
553
@DOCSTRING(isprint)
 
554
 
 
555
@DOCSTRING(isascii)
 
556
 
 
557
@DOCSTRING(isstrprop)