#64 – Escape Sequences in String Literals
August 20, 2010 2 Comments
C# allows embedding special (often non-printable) characters into a string literal using an escape sequence. An escape sequence is a series of characters that begins with a backslash (\), followed by one or more letters or digits.
Here’s an example of embedding several newline characters into a string, so that it’s printed on three different lines.
Console.Write("First line\nSecond line\nThird line\n"); // 3 lines
Full list of escape sequences in C#:
- \a – Bell (alert)
- \b – Backspace
- \f – Formfeed
- \n – New line
- \r – Carriage return
- \t – Horizontal tab
- \v – Vertical tab
- \’ – Single quote
- \” – Double quote
- \\ – Backslash
- (Backslash followed by 0) – Null
- \xhh – ASCII character in hex
- \xhhhh – Unicode character in hex
- \uhhhh – Unicode character (4-byte)
- \Uhhhhhhhh – Unicode surrogate pair (8-byte)