Strings
*******

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 Arithmetic Ops) 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.

\\
     Represents a literal backslash, \.

\"
     Represents a literal double-quote character, ".

\'
     Represents a literal single-quote character, '.

\0
     Represents the ``nul'' character, control-@, ASCII code 0.

\a
     Represents the ``alert'' character, control-g, ASCII code 7.

\b
     Represents a backspace, control-h, ASCII code 8.

\f
     Represents a formfeed, control-l, ASCII code 12.

\n
     Represents a newline, control-j, ASCII code 10.

\r
     Represents a carriage return, control-m, ASCII code 13.

\t
     Represents a horizontal tab, control-i, ASCII code 9.

\v
     Represents a vertical tab, control-k, ASCII code 11.

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 Numeric Data Types, for more information about creating matrices.

Creating Strings
================

 -- Function File: blanks (N)
     Return a string of N blanks.

 -- Built-in Function: char (X)
 -- Built-in Function: char (CELL_ARRAY)
 -- Built-in Function: char (S1, S2, ...)
     Create a string array from a numeric matrix, cell array, or list of

     If the argument is a numeric matrix, each element of the matrix is converted to the corresponding ASCII character.  For example,

          char ([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.

 -- Function File: int2str (N)
 -- Function File: num2str (X, PRECISION)
 -- Function File: num2str (X, FORMAT)
     Convert a number to a string.  These functions are not very flexible, but are provided for compatibility with MATLAB.  For better control over the results, use sprintf (see Formatted Output).

sprintf and num2str

 -- Function File: com2str (ZZ, FLG)
     This function has been deprecated.  Use num2str instead.

     Convert complex number to a string. Inputs

     zz
          complex number

     flg
          format flag 0 (default):            -1, 0, 1,   1i,   1 + 0.5i 1 (for use with zpout): -1, 0, + 1, + 1i, + 1 + 0.5i

 -- Function File: strcat (S1, S2, ...)
     Return a string containing all the arguments concatenated.  For example,

          s = [ "ab"; "cde" ];
          strcat (s, s, s)
          => "ab ab ab "
                  "cdecdecde"

 -- Built-in Variable: string_fill_char
     The value of this variable is used to pad all strings in a string matrix to the same length.  It should be a single character.  The default value is " " (a single space).  For example,

          string_fill_char = "X";
          [ "these"; "are"; "strings" ]
               => "theseXX"
                  "areXXXX"
                  "strings"

 -- Function File: str2mat (S_1, ..., S_N)
     Return a matrix containing the strings S_1, ..., S_N as its rows.  Each string is padded with blanks in order to form a valid matrix.

     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.

 -- Built-in Function: ischar (A)
     Return 1 if A is a string.  Otherwise, return 0.

 -- Function File: isstr (A)
     This function has been deprecated.  Use ischar instead.

Searching and Replacing
=======================

 -- Function File: deblank (S)
     Removes the trailing blanks and nulls from the string S. If S is a matrix, DEBLANK trims each row to the length of longest string.

 -- Function File: findstr (S, T, OVERLAP)
     Return the vector of all positions in the longer of the two strings S and T where an occurrence of the shorter of the two starts. If the optional argument OVERLAP is nonzero, the returned vector can include overlapping positions (this is the default).  For example,

          findstr ("ababab", "a")
          => [ 1, 3, 5 ]
          findstr ("abababa", "aba", 0)
          => [ 1, 5 ]

 -- Function File: index (S, T)
     Return the position of the first occurrence of the string T in the string S, or 0 if no occurrence is found.  For example,

          index ("Teststring", "t")
          => 4

     Caution:  This function does not work for arrays of strings.

 -- Function File: rindex (S, T)
     Return the position of the last occurrence of the string T in the string S, or 0 if no occurrence is found.  For example,

          rindex ("Teststring", "t")
          => 6

     Caution:  This function does not work for arrays of strings.

 -- Function File: split (S, T)
     Divides the string S into pieces separated by T, returning the result in a string array (padded with blanks to form a valid matrix).  For example,

          split ("Test string", "t")
          => "Tes "
                  " s  "
                  "ring"

 -- Function File: strcmp (S1, S2)
     Compares two character strings, returning true if they are the same, and false otherwise.

     Caution:  For compatibility with MATLAB, Octave's strcmp function returns true if the character strings are equal, and false otherwise.  This is just the opposite of the corresponding C library function.

 -- Function File: strrep (S, X, Y)
     Replaces all occurrences of the substring X of the string S with the string Y.  For example,

          strrep ("This is a test string", "is", "&%$")
          => "Th&%$ &%$ a test string"

 -- Function File: substr (S, BEG, LEN)
     Return the substring of S which starts at character number BEG and is LEN characters long.

     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"

          This function is patterned after AWK.  You can get the same result by S (BEG : (BEG + LEN - 1)).

String Conversions
==================

 -- Function File: hex2dec (S)
     Return the decimal number corresponding to the binary number stored in the string S.  For example,

          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.

dec2hex, base2dec, dec2base, bin2dec, dec2bin

 -- Function File: dec2bin (N, LEN)
     Return a binary number corresponding the nonnegative decimal number N, as a string of ones and zeros.  For example,

          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.

     The optional second argument, LEN, specifies the minimum number of digits in the result.

bin2dec, dec2base, base2dec, hex2dec, dec2hex

 -- Function File: dec2hex (N, LEN)
     Return the hexadecimal string corresponding to the nonnegative integer N.  For example,

          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.

     The optional second argument, LEN, specifies the minimum number of digits in the result.

hex2dec, dec2base, base2dec, bin2dec, dec2bin

 -- Function File: hex2dec (S)
     Returns the integer corresponding to the hexadecimal number stored in the string S.  For example,

          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.

dec2hex, base2dec, dec2base, bin2dec, dec2bin

 -- Function File: dec2base (N, B, LEN)
     Return a string of symbols in base B corresponding to the the nonnegative integer N.

          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"

     The optional third argument, LEN, specifies the minimum number of digits in the result.

base2dec, dec2bin, bin2dec, hex2dec, dec2hex

 -- Function File: base2dec (S, B)
     Convert S from a string of digits of base B into an integer.

          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

dec2base, dec2bin, bin2dec, hex2dec, dec2hex

 -- Function File: strjust (S, ["left"|"right"|"center"])
     Shift the non-blank text of S to the left, right or center of the string.  If S is a string array, justify each string in the array.  Null characters are replaced by blanks.  If no justification is specified, then all rows are right-justified.

 -- Function File: str2num (S)
     Convert the string S to a number.

 -- Mapping Function: toascii (S)
     Return ASCII representation of S in a matrix.  For example,

          toascii ("ASCII")
               => [ 65, 83, 67, 73, 73 ]

 -- Mapping Function: tolower (S)
     Return a copy of the string S, with each upper-case character replaced by the corresponding lower-case one; nonalphabetic characters are left unchanged.  For example,

          tolower ("MiXeD cAsE 123")
               => "mixed case 123"

 -- Built-in Function: toupper (S)
     Return a copy of the string S, with each  lower-case character replaced by the corresponding upper-case one; nonalphabetic characters are left unchanged.  For example,

          toupper ("MiXeD cAsE 123")
               => "MIXED CASE 123"

 -- Built-in Function: do_string_escapes (STRING)
     Convert special characters in STRING to their escaped forms.

 -- Built-in Function: undo_string_escapes (S)
     Converts special characters in strings back to their escaped forms.  For example, the expression

          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.

 -- Built-in Variable: warn_num_to_str
     If the value of warn_num_to_str is nonzero, a warning is printed for implicit conversions of numbers to their ASCII character equivalents when strings are constructed using a mixture of strings and numbers in matrix notation.  For example,

          [ "f", 111, 111 ]
               => "foo"

     elicits a warning if warn_num_to_str is nonzero.  The default value is 1.

 -- Built-in Variable: warn_str_to_num
     If the value of warn_str_to_num is nonzero, a warning is printed for implicit conversions of strings to their numeric ASCII equivalents. For example,

          "abc" + 0
               => 97 98 99

     elicits a warning if warn_str_to_num is nonzero.  The default value is 0.

 -- Built-in Variable: warn_single_quote_string
     Print warning if a signle quote character is used to introduce a string constant.

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,

     isalpha ("!Q@WERT^Y&")
          => [ 0, 1, 0, 1, 1, 1, 1, 0, 1, 0 ]

 -- Mapping Function: isalnum (S)
     Return 1 for characters that are letters or digits (isalpha (S) or isdigit (S) is true).

 -- Mapping Function: isalpha (S)
 -- Mapping Function: isletter (S)
     Return true for characters that are letters (isupper (S) or islower (S) is true).

 -- Mapping Function: isascii (S)
     Return 1 for characters that are ASCII (in the range 0 to 127 decimal).

 -- Mapping Function: iscntrl (S)
     Return 1 for control characters.

 -- Mapping Function: isdigit (S)
     Return 1 for characters that are decimal digits.

 -- Mapping Function: isgraph (S)
     Return 1 for printable characters (but not the space character).

 -- Mapping Function: islower (S)
     Return 1 for characters that are lower case letters.

 -- Mapping Function: isprint (S)
     Return 1 for printable characters (including the space character).

 -- Mapping Function: ispunct (S)
     Return 1 for punctuation characters.

 -- Mapping Function: isspace (S)
     Return 1 for whitespace characters (space, formfeed, newline, carriage return, tab, and vertical tab).

 -- Mapping Function: isupper (S)
     Return 1 for upper case letters.

 -- Mapping Function: isxdigit (S)
     Return 1 for characters that are hexadecimal digits.