<html lang="en"> <head> <title>Escape Sequences in String Constants - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Strings.html#Strings" title="Strings"> <link rel="next" href="Character-Arrays.html#Character-Arrays" title="Character Arrays"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Escape-Sequences-in-String-Constants"></a> <p> Next: <a rel="next" accesskey="n" href="Character-Arrays.html#Character-Arrays">Character Arrays</a>, Up: <a rel="up" accesskey="u" href="Strings.html#Strings">Strings</a> <hr> </div> <h3 class="section">5.1 Escape Sequences in String Constants</h3> <p><a name="index-escape-sequence-notation-325"></a>In double-quoted strings, the backslash character is used to introduce <dfn>escape sequences</dfn> that represent other characters. For example, ‘<samp><span class="samp">\n</span></samp>’ embeds a newline character in a double-quoted string and ‘<samp><span class="samp">\"</span></samp>’ embeds a double quote character. In single-quoted strings, backslash is not a special character. Here is an example showing the difference: <pre class="example"> toascii ("\n") ⇒ 10 toascii ('\n') ⇒ [ 92 110 ] </pre> <p>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. <dl> <dt><code>\\</code><dd>Represents a literal backslash, ‘<samp><span class="samp">\</span></samp>’. <br><dt><code>\"</code><dd>Represents a literal double-quote character, ‘<samp><span class="samp">"</span></samp>’. <br><dt><code>\'</code><dd>Represents a literal single-quote character, ‘<samp><span class="samp">'</span></samp>’. <br><dt><code>\0</code><dd>Represents the “nul” character, control-@, ASCII code 0. <br><dt><code>\a</code><dd>Represents the “alert” character, control-g, ASCII code 7. <br><dt><code>\b</code><dd>Represents a backspace, control-h, ASCII code 8. <br><dt><code>\f</code><dd>Represents a formfeed, control-l, ASCII code 12. <br><dt><code>\n</code><dd>Represents a newline, control-j, ASCII code 10. <br><dt><code>\r</code><dd>Represents a carriage return, control-m, ASCII code 13. <br><dt><code>\t</code><dd>Represents a horizontal tab, control-i, ASCII code 9. <br><dt><code>\v</code><dd>Represents a vertical tab, control-k, ASCII code 11. <!-- We don't do octal or hex this way yet. --> <!-- @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}.@refill --> <!-- @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. (The --> <!-- @samp{\x} escape sequence is not allowed in @sc{posix} @code{awk}.)@refill --> </dl> <p>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, <pre class="example"> 'I can''t escape' ⇒ I can't escape </pre> <p>In scripts the two different string types can be distinguished if necessary by using <code>is_dq_string</code> and <code>is_sq_string</code>. <!-- is_dq_string src/ov.cc --> <p><a name="doc_002dis_005fdq_005fstring"></a> <div class="defun"> — Built-in Function: <b>is_dq_string</b> (<var>x</var>)<var><a name="index-is_005fdq_005fstring-326"></a></var><br> <blockquote><p>Return true if <var>x</var> is a double-quoted character string. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dis_005fsq_005fstring.html#doc_002dis_005fsq_005fstring">is_sq_string</a>, <a href="doc_002dischar.html#doc_002dischar">ischar</a>. </p></blockquote></div> <!-- is_sq_string src/ov.cc --> <p><a name="doc_002dis_005fsq_005fstring"></a> <div class="defun"> — Built-in Function: <b>is_sq_string</b> (<var>x</var>)<var><a name="index-is_005fsq_005fstring-327"></a></var><br> <blockquote><p>Return true if <var>x</var> is a single-quoted character string. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dis_005fdq_005fstring.html#doc_002dis_005fdq_005fstring">is_dq_string</a>, <a href="doc_002dischar.html#doc_002dischar">ischar</a>. </p></blockquote></div> </body></html>