Next: , Previous: Numeric Data Types, Up: Top


5 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.