A string constant consists of a sequence of characters enclosed in either double-quote or single-quote marks. For example, both of the following expressions
"parrot" 'parrot'
represent the string whose contents are `parrot'. Strings in Octave can be of any length.
Since the single-quote mark is also used for the transpose operator (see section Arithmetic Operators) but double-quote marks have no other purpose in Octave, it is best to use double-quote marks to denote strings.
Some characters cannot be included literally in a string constant. You represent them instead with escape sequences, which are character sequences beginning with a backslash (`\').
One use of an escape sequence is to include a double-quote
(single-quote) character in a string constant that has been defined
using double-quote (single-quote) marks. Since a plain double-quote
would end the string, you must use `\"' to represent a single
double-quote character as a part of the string. The backslash character
itself is another character that cannot be included normally. You must
write `\\' to put one backslash in the string. Thus, the string
whose contents are the two characters `"\' may be written
"\"\\"
or '"\\'
. Similarly, the string whose contents are
the two characters `'\' may be written '\'\\'
or "'\\"
.
Another use of backslash is to represent unprintable characters such as newline. While there is nothing to stop you from writing most of these characters directly in a string constant, they may look ugly.
Here is a table of all the escape sequences used in Octave. They are the same as those used in the C programming language.
\\
\"
\'
\0
\a
\b
\f
\n
\r
\t
\v
Strings may be concatenated using the notation for defining matrices. For example, the expression
[ "foo" , "bar" , "baz" ]
produces the string whose contents are `foobarbaz'. See section Numeric Data Types, for more information about creating matrices.
@anchor{doc-blanks}
@anchor{doc-char}
If the argument is a numeric matrix, each element of the matrix is converted to the corresponding ASCII character. For example,
setstr ([97, 98, 99]) => "abc"
If the argument is a cell array of strings, the result is a string array with each element corresponding to one element of the cell array.
For multiple string arguments, the result is a string array with each element corresponding to the arguments.
The returned values are padded with blanks as needed to make each row of the string array have the same length.
@anchor{doc-int2str}
sprintf
(see section Formatted Output).
@anchor{doc-com2str}
convert complex number to a string Inputs
@anchor{doc-strcat}
s = [ "ab"; "cde" ]; strcat (s, s, s) => "ab ab ab " "cdecdecde"
@anchor{doc-string_fill_char}
" "
(a single space). For example,
string_fill_char = "X"; [ "these"; "are"; "strings" ] => "theseXX" "areXXXX" "strings"
@anchor{doc-str2mat}
Note:
This function is modelled after MATLAB. In Octave, you can create
a matrix of strings by [s_1; ...; s_n]
even if
the strings are not all the same length.
@anchor{doc-isstr}
@anchor{doc-deblank}
@anchor{doc-findstr}
findstr ("ababab", "a") => [ 1, 3, 5 ] findstr ("abababa", "aba", 0) => [ 1, 5 ]
@anchor{doc-index}
index ("Teststring", "t") => 4
Note: This function does not work for arrays of strings.
@anchor{doc-rindex}
rindex ("Teststring", "t") => 6
Note: This function does not work for arrays of strings.
@anchor{doc-split}
split ("Test string", "t") => "Tes " " s " "ring"
@anchor{doc-strcmp}
Note: For compatibility with MATLAB, Octave's strcmp function returns 1 if the strings are equal, and 0 otherwise. This is just the opposite of the corresponding C library function.
@anchor{doc-strrep}
strrep ("This is a test string", "is", "&%$") => "Th&%$ &%$ a test string"
@anchor{doc-substr}
If OFFSET is negative, extraction starts that far from the end of the string. If LEN is omitted, the substring extends to the end of S.
For example,
substr ("This is a test string", 6, 9) => "is a test"
Note: This function is patterned after AWK. You can get the same result by
s (beg : (beg + len - 1))
.
@anchor{doc-bin2dec}
hex2dec ("1110") => 14
If s is a string matrix, returns a column vector of converted numbers, one per row of s. Invalid rows evaluate to NaN.
@seealso{dec2hex, base2dec, dec2base, bin2dec, dec2bin}
@anchor{doc-dec2bin}
dec2bin (14) => "1110"
If n is a vector, returns a string matrix, one row per value, padded with leading zeros to the width of the largest value.
@seealso{bin2dec, dec2base, base2dec, hex2dec, dec2hex}
@anchor{doc-dec2hex}
dec2hex (2748) => "ABC"
If n is a vector, returns a string matrix, one row per value, padded with leading zeros to the width of the largest value.
@seealso{hex2dec, dec2base, base2dec, bin2dec, dec2bin}
@anchor{doc-hex2dec}
hex2dec ("12B") => 299 hex2dec ("12b") => 299
If s is a string matrix, returns a column vector of converted numbers, one per row of s. Invalid rows evaluate to NaN.
@seealso{dec2hex, base2dec, dec2base, bin2dec, dec2bin}
@anchor{doc-dec2base}
dec2base (123, 3) => "11120"
If n is a vector, return a string matrix with one row per value, padded with leading zeros to the width of the largest value.
If b is a string then the characters of b are used as the symbols for the digits of n. Space (' ') may not be used as a symbol.
dec2base (123, "aei") => "eeeia"
@seealso{base2dec, dec2bin, bin2dec, hex2dec, dec2hex}
@anchor{doc-base2dec}
base2dec ("11120", 3) => 123
If s is a matrix, returns a column vector with one value per row of s. If a row contains invalid symbols then the corresponding value will be NaN. Rows are right-justified before converting so that trailing spaces are ignored.
If b is a string, the characters of b are used as the symbols for the digits of s. Space (' ') may not be used as a symbol.
base2dec ("yyyzx", "xyz") => 123
@seealso{dec2base, dec2bin, bin2dec, hex2dec, dec2hex}
@anchor{doc-strjust}
@anchor{doc-str2num}
@anchor{doc-toascii}
toascii ("ASCII") => [ 65, 83, 67, 73, 73 ]
@anchor{doc-tolower}
tolower ("MiXeD cAsE 123") => "mixed case 123"
@anchor{doc-toupper}
toupper ("MiXeD cAsE 123") => "MIXED CASE 123"
@anchor{doc-do_string_escapes}
@anchor{doc-undo_string_escapes}
bell = "\a";
assigns the value of the alert character (control-g, ASCII code 7) to
the string variable bell
. If this string is printed, the
system will ring the terminal bell (if it is possible). This is
normally the desired outcome. However, sometimes it is useful to be
able to print the original representation of the string, with the
special characters replaced by their escape sequences. For example,
octave:13> undo_string_escapes (bell) ans = \a
replaces the unprintable alert character with its printable representation.
@anchor{doc-implicit_num_to_str_ok}
implicit_num_to_str_ok
is nonzero, implicit
conversions of numbers to their ASCII character equivalents are
allowed when strings are constructed using a mixture of strings and
numbers in matrix notation. Otherwise, an error message is printed and
control is returned to the top level. The default value is 0. For
example,
[ "f", 111, 111 ] => "foo"
@anchor{doc-implicit_str_to_num_ok}
implicit_str_to_num_ok
is nonzero, implicit
conversions of strings to their numeric ASCII equivalents are allowed.
Otherwise, an error message is printed and control is returned to the
top level. The default value is 0.
@anchor{doc-warn_single_quote_string}
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,
isalpha ("!Q@WERT^Y&") => [ 0, 1, 0, 1, 1, 1, 1, 0, 1, 0 ]
@anchor{doc-isalnum}
isalpha
(s)
or isdigit (s)
is true).
@anchor{doc-isalpha}
isupper (s)
or islower (s)
is true).
@anchor{doc-isascii}
@anchor{doc-iscntrl}
@anchor{doc-isdigit}
@anchor{doc-isgraph}
@anchor{doc-islower}
@anchor{doc-isprint}
@anchor{doc-ispunct}
@anchor{doc-isspace}
@anchor{doc-isupper}
@anchor{doc-isxdigit}
Go to the first, previous, next, last section, table of contents.