What character escape sequences are available?


C# defines the following character escape sequences:

  • \' – single quote, needed for character literals
  • \" – double quote, needed for string literals
  • \\ – backslash
  • \0 – Unicode character 0
  • \a – Alert (character 7)
  • \b – Backspace (character 8)
  • \f – Form feed (character 12)
  • \n – New line (character 10)
  • \r – Carriage return (character 13)
  • \t – Horizontal tab (character 9)
  • \v – Vertical quote (character 11)
  • \uxxxx – Unicode escape sequence for character with hex value xxxx
  • \xn[n][n][n] – Unicode escape sequence for character with hex value nnnn (variable length version of \uxxxx)
  • \Uxxxxxxxx – Unicode escape sequence for character with hex value xxxxxxxx (for generating surrogates)

Of these, \a, \f, \v, \x and \U are rarely used in my experience.

[Author: Jon Skeet]




1 comment

Leave a comment

  • Solomon Rutzky
    Solomon Rutzky

    Regarding the \x, \u, and \U escape sequences:
    1) Be careful when using “\x” with less than 4 hex digits IF the character immediately following the escape sequence is a valid hex digit. In this case, the character that immediately follows will be interpreted as being part of the escape sequence, which will return an unexpected character

    2) The “\u” escape takes a UTF-16 code unit (0000 – FFFF), and the first 65,536 code units are also the same values as the corresponding Unicode code points. Two “\u” sequences containing surrogate code units (i.e. a surrogate pair) can be used to create supplementary characters (i.e. the remaining code points beyond FFFF: 010000 – 10FFFF).

    3) The “\U” information above is incorrect. This escape has nothing to do with surrogate pairs. The “\U” escape is for specifying a Unicode code point or UTF-32 code unit (same thing, really). The first two hex digits are always 0, so the escape sequence is really: “\U00xxxxxx“. The range for the “x“s is: 000000 – 10FFFF.

    For more details, including working examples, please see:
    Unicode Escape Sequences Across Various Languages and Platforms (including Supplementary Characters)

    Take care,Solomon….