5.1 Escape Sequences in String Constants
In double-quoted strings, the backslash character is used to introduce escape sequences that represent other characters. For example, ‘\n’ embeds a newline character in a double-quoted string and ‘\"’ embeds a double quote character. In single-quoted strings, backslash is not a special character. Here is an example showing the difference:
double ("\n") ⇒ 10 double ('\n') ⇒ [ 92 110 ]
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.
\\
-
Represents a literal backslash, ‘\’.
\"
-
Represents a literal double-quote character, ‘"’.
\'
-
Represents a literal single-quote character, ‘'’.
\0
-
Represents the null 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.
\nnn
-
Represents the octal value nnn, where nnn are one to three digits between 0 and 7. For example, the code for the ASCII ESC (escape) character is ‘\033’.
\xhh…
Represents the hexadecimal value hh, where hh are hexadecimal digits (‘0’ through ‘9’ and either ‘A’ through ‘F’ or ‘a’ through ‘f’). Like the same construct in ANSI C, the escape sequence continues until the first non-hexadecimal digit is seen. However, using more than two hexadecimal digits produces undefined results.
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,
'I can''t escape' ⇒ I can't escape
In scripts the two different string types can be distinguished if necessary by using is_dq_string
and is_sq_string
.
- : is_dq_string (x)
-
Return true if x is a double-quoted character string.
See also: is_sq_string, ischar.
- : is_sq_string (x)
-
Return true if x is a single-quoted character string.
See also: is_dq_string, ischar.
© 1996–2020 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/v6.3.0/Escape-Sequences-in-String-Constants.html