#64 – Escape Sequences in String Literals

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)
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

2 Responses to #64 – Escape Sequences in String Literals

  1. Pingback: #65 – Verbatim String Literals « 2,000 Things You Should Know About C#

  2. Peter Chamberlin says:

    The string escape for null is not rendering on the page as . There’s also a mixture of hyphen and en-dash.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: