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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
|
@c Copyright (C) 1996-2013 John W. Eaton
@c
@c This file is part of Octave.
@c
@c Octave is free software; you can redistribute it and/or modify it
@c under the terms of the GNU General Public License as published by the
@c Free Software Foundation; either version 3 of the License, or (at
@c your option) any later version.
@c
@c Octave is distributed in the hope that it will be useful, but WITHOUT
@c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
@c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
@c for more details.
@c
@c You should have received a copy of the GNU General Public License
@c along with Octave; see the file COPYING. If not, see
@c <http://www.gnu.org/licenses/>.
@node Strings
@chapter Strings
@cindex strings
@cindex character strings
@opindex "
@opindex '
A @dfn{string constant} consists of a sequence of characters enclosed in
either double-quote or single-quote marks. For example, both of the
following expressions
@example
@group
"parrot"
'parrot'
@end group
@end example
@noindent
represent the string whose contents are @samp{parrot}. Strings in
Octave can be of any length.
Since the single-quote mark is also used for the transpose operator
(@pxref{Arithmetic Ops}) but double-quote marks have no other purpose in Octave,
it is best to use double-quote marks to denote strings.
Strings can be concatenated using the notation for defining matrices. For
example, the expression
@example
[ "foo" , "bar" , "baz" ]
@end example
@noindent
produces the string whose contents are @samp{foobarbaz}. @xref{Numeric Data
Types}, for more information about creating matrices.
@menu
* Escape Sequences in String Constants::
* Character Arrays::
* Creating Strings::
* Comparing Strings::
* Manipulating Strings::
* String Conversions::
* Character Class Functions::
@end menu
@node Escape Sequences in String Constants
@section Escape Sequences in String Constants
@cindex escape sequence notation
In double-quoted strings, the backslash character is used to introduce
@dfn{escape sequences} that represent other characters. For example,
@samp{\n} embeds a newline character in a double-quoted string and
@samp{\"} embeds a double quote character. In single-quoted strings, backslash
is not a special character. Here is an example showing the difference:
@example
@group
toascii ("\n")
@result{} 10
toascii ('\n')
@result{} [ 92 110 ]
@end group
@end example
Here is a table of all the escape sequences used in Octave (within
double quoted strings). They are the same as those used in the C
programming language.
@table @code
@item \\
Represents a literal backslash, @samp{\}.
@item \"
Represents a literal double-quote character, @samp{"}.
@item \'
Represents a literal single-quote character, @samp{'}.
@item \0
Represents the null character, control-@@, ASCII code 0.
@item \a
Represents the ``alert'' character, control-g, ASCII code 7.
@item \b
Represents a backspace, control-h, ASCII code 8.
@item \f
Represents a formfeed, control-l, ASCII code 12.
@item \n
Represents a newline, control-j, ASCII code 10.
@item \r
Represents a carriage return, control-m, ASCII code 13.
@item \t
Represents a horizontal tab, control-i, ASCII code 9.
@item \v
Represents a vertical tab, control-k, ASCII code 11.
@item \@var{nnn}
Represents the octal value @var{nnn}, where @var{nnn} are one to three
digits between 0 and 7. For example, the code for the ASCII ESC
(escape) character is @samp{\033}.
@item \x@var{hh}@dots{}
Represents the hexadecimal value @var{hh}, where @var{hh} are hexadecimal
digits (@samp{0} through @samp{9} and either @samp{A} through @samp{F} or
@samp{a} through @samp{f}). Like the same construct in @sc{ansi} C,
the escape sequence continues until the first non-hexadecimal digit is seen.
However, using more than two hexadecimal digits produces undefined results.
@end table
In a single-quoted string there is only one escape sequence: you may insert a
single quote character using two single quote characters in succession. For
example,
@example
@group
'I can''t escape'
@result{} I can't escape
@end group
@end example
In scripts the two different string types can be distinguished if necessary
by using @code{is_dq_string} and @code{is_sq_string}.
@DOCSTRING(is_dq_string)
@DOCSTRING(is_sq_string)
@node Character Arrays
@section Character Arrays
The string representation used by Octave is an array of characters, so
internally the string @nospell{@qcode{"dddddddddd"}} is actually a row vector
of length 10 containing the value 100 in all places (100 is the ASCII code of
@qcode{"d"}). This lends itself to the obvious generalization to character
matrices. Using a matrix of characters, it is possible to represent a
collection of same-length strings in one variable. The convention used in
Octave is that each row in a character matrix is a separate string, but letting
each column represent a string is equally possible.
The easiest way to create a character matrix is to put several strings
together into a matrix.
@example
collection = [ "String #1"; "String #2" ];
@end example
@noindent
This creates a 2-by-9 character matrix.
The function @code{ischar} can be used to test if an object is a character
matrix.
@DOCSTRING(ischar)
To test if an object is a string (i.e., a character vector and not a character
matrix) you can use the @code{ischar} function in combination with the
@code{isvector} function as in the following example:
@example
@group
ischar (collection)
@result{} 1
ischar (collection) && isvector (collection)
@result{} 0
ischar ("my string") && isvector ("my string")
@result{} 1
@end group
@end example
One relevant question is, what happens when a character matrix is
created from strings of different length. The answer is that Octave
puts blank characters at the end of strings shorter than the longest
string. It is possible to use a different character than the
blank character using the @code{string_fill_char} function.
@DOCSTRING(string_fill_char)
This shows a problem with character matrices. It simply isn't possible to
represent strings of different lengths. The solution is to use a cell array of
strings, which is described in @ref{Cell Arrays of Strings}.
@node Creating Strings
@section Creating Strings
The easiest way to create a string is, as illustrated in the introduction,
to enclose a text in double-quotes or single-quotes. It is however
possible to create a string without actually writing a text. The
function @code{blanks} creates a string of a given length consisting
only of blank characters (ASCII code 32).
@DOCSTRING(blanks)
@menu
* Concatenating Strings::
* Converting Numerical Data to Strings::
@end menu
@node Concatenating Strings
@subsection Concatenating Strings
Strings can be concatenated using matrix notation
(@pxref{Strings}, @ref{Character Arrays}) which is often the most natural
method. For example:
@example
@group
fullname = [fname ".txt"];
email = ["<" user "@@" domain ">"];
@end group
@end example
@noindent
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.
Nevertheless, there are several other functions for concatenating string
objects which can be useful in specific circumstances: @code{char},
@code{strvcat}, @code{strcat}, and @code{cstrcat}. Finally, the general
purpose concatenation functions can be used: see @ref{XREFcat,,cat},
@ref{XREFhorzcat,,horzcat}, and @ref{XREFvertcat,,vertcat}.
@itemize @bullet
@item All string concatenation functions except @code{cstrcat}
convert numerical input into character data by taking the corresponding ASCII
character for each element, as in the following example:
@example
@group
char ([98, 97, 110, 97, 110, 97])
@result{} banana
@end group
@end example
@item
@code{char} and @code{strvcat}
concatenate vertically, while @code{strcat} and @code{cstrcat} concatenate
horizontally. For example:
@example
@group
char ("an apple", "two pears")
@result{} an apple
two pears
@end group
@group
strcat ("oc", "tave", " is", " good", " for you")
@result{} octave is good for you
@end group
@end example
@item @code{char} generates an empty row in the output
for each empty string in the input. @code{strvcat}, on the other hand,
eliminates empty strings.
@example
@group
char ("orange", "green", "", "red")
@result{} orange
green
red
@end group
@group
strvcat ("orange", "green", "", "red")
@result{} orange
green
red
@end group
@end example
@item All string concatenation functions except @code{cstrcat} also accept cell
array data (@pxref{Cell Arrays}). @code{char} and
@code{strvcat} convert cell arrays into character arrays, while @code{strcat}
concatenates within the cells of the cell arrays:
@example
@group
char (@{"red", "green", "", "blue"@})
@result{} red
green
blue
@end group
@group
strcat (@{"abc"; "ghi"@}, @{"def"; "jkl"@})
@result{}
@{
[1,1] = abcdef
[2,1] = ghijkl
@}
@end group
@end example
@item @code{strcat} removes trailing white space in the arguments (except
within cell arrays), while @code{cstrcat} leaves white space untouched. Both
kinds of behavior can be useful as can be seen in the examples:
@example
@group
strcat (["dir1";"directory2"], ["/";"/"], ["file1";"file2"])
@result{} dir1/file1
directory2/file2
@end group
@group
cstrcat (["thirteen apples"; "a banana"], [" 5$";" 1$"])
@result{} thirteen apples 5$
a banana 1$
@end group
@end example
Note that in the above example for @code{cstrcat}, the white space originates
from the internal representation of the strings in a string array
(@pxref{Character Arrays}).
@end itemize
@DOCSTRING(char)
@DOCSTRING(strvcat)
@DOCSTRING(strcat)
@DOCSTRING(cstrcat)
@node Converting Numerical Data to Strings
@subsection Converting Numerical Data to Strings
Apart from the string concatenation functions (@pxref{Concatenating Strings})
which cast numerical data to the corresponding ASCII characters, there are
several functions that format numerical data as strings. @code{mat2str} and
@code{num2str} convert real or complex matrices, while @code{int2str} converts
integer matrices. @code{int2str} takes the real part of complex values and
round fractional values to integer. A more flexible way to format numerical
data as strings is the @code{sprintf} function (@pxref{Formatted Output},
@ref{XREFsprintf,,sprintf}).
@DOCSTRING(mat2str)
@DOCSTRING(num2str)
@DOCSTRING(int2str)
@node Comparing Strings
@section Comparing Strings
Since a string is a character array, comparisons between strings work
element by element as the following example shows:
@example
@group
GNU = "GNU's Not UNIX";
spaces = (GNU == " ")
@result{} spaces =
0 0 0 0 0 1 0 0 0 1 0 0 0 0
@end group
@end example
@noindent To determine if two strings are identical it is necessary to use the
@code{strcmp} function. It compares complete strings and is case
sensitive. @code{strncmp} compares only the first @code{N} characters (with
@code{N} given as a parameter). @code{strcmpi} and @code{strncmpi} are the
corresponding functions for case-insensitive comparison.
@DOCSTRING(strcmp)
@DOCSTRING(strncmp)
@DOCSTRING(strcmpi)
@DOCSTRING(strncmpi)
@DOCSTRING(validatestring)
@node Manipulating Strings
@section Manipulating Strings
Octave supports a wide range of functions for manipulating strings.
Since a string is just a matrix, simple manipulations can be accomplished
using standard operators. The following example shows how to replace
all blank characters with underscores.
@example
@group
quote = ...
"First things first, but not necessarily in that order";
quote( quote == " " ) = "_"
@result{} quote =
First_things_first,_but_not_necessarily_in_that_order
@end group
@end example
For more complex manipulations, such as searching, replacing, and
general regular expressions, the following functions come with Octave.
@DOCSTRING(deblank)
@DOCSTRING(strtrim)
@DOCSTRING(strtrunc)
@DOCSTRING(findstr)
@DOCSTRING(strchr)
@DOCSTRING(index)
@DOCSTRING(rindex)
@DOCSTRING(strfind)
@DOCSTRING(strjoin)
@DOCSTRING(strmatch)
@DOCSTRING(strtok)
@DOCSTRING(strsplit)
@DOCSTRING(ostrsplit)
@DOCSTRING(strread)
@DOCSTRING(strrep)
@DOCSTRING(substr)
@DOCSTRING(regexp)
@DOCSTRING(regexpi)
@DOCSTRING(regexprep)
@DOCSTRING(regexptranslate)
@DOCSTRING(untabify)
@node String Conversions
@section String Conversions
Octave supports various kinds of conversions between strings and
numbers. As an example, it is possible to convert a string containing
a hexadecimal number to a floating point number.
@example
@group
hex2dec ("FF")
@result{} 255
@end group
@end example
@DOCSTRING(bin2dec)
@DOCSTRING(dec2bin)
@DOCSTRING(dec2hex)
@DOCSTRING(hex2dec)
@DOCSTRING(dec2base)
@DOCSTRING(base2dec)
@DOCSTRING(num2hex)
@DOCSTRING(hex2num)
@DOCSTRING(str2double)
@DOCSTRING(strjust)
@DOCSTRING(str2num)
@DOCSTRING(toascii)
@DOCSTRING(tolower)
@DOCSTRING(toupper)
@DOCSTRING(do_string_escapes)
@DOCSTRING(undo_string_escapes)
@node Character Class Functions
@section Character Class Functions
Octave also provides the following character class test functions
patterned after the functions in the standard C library. They all
operate on string arrays and return matrices of zeros and ones.
Elements that are nonzero indicate that the condition was true for the
corresponding character in the string array. For example:
@example
@group
isalpha ("!Q@@WERT^Y&")
@result{} [ 0, 1, 0, 1, 1, 1, 1, 0, 1, 0 ]
@end group
@end example
@DOCSTRING(isalnum)
@DOCSTRING(isalpha)
@DOCSTRING(isletter)
@DOCSTRING(islower)
@DOCSTRING(isupper)
@DOCSTRING(isdigit)
@DOCSTRING(isxdigit)
@DOCSTRING(ispunct)
@DOCSTRING(isspace)
@DOCSTRING(iscntrl)
@DOCSTRING(isgraph)
@DOCSTRING(isprint)
@DOCSTRING(isascii)
@DOCSTRING(isstrprop)
|