Stats

175 Contributors: 11 Monday, February 27, 2017
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

String Escape Sequences

Syntax

  • \' — single quote (0x0027)
  • \" — double quote (0x0022)
  • \\ — backslash (0x005C)
  • \0 — null (0x0000)
  • \a — alert (0x0007)
  • \b — backspace (0x0008)
  • \f — form feed (0x000C)
  • \n — new line (0x000A)
  • \r — carriage return (0x000D)
  • \t — horizontal tab (0x0009)
  • \v — vertical tab (0x000B)
  • \u0000 - \uFFFF — Unicode character
  • \x0 - \xFFFF — Unicode character (code with variable length)
  • \U00000000 - \U0010FFFF — Unicode character (for generating surrogates)

Remarks

String escape sequences are transformed to the corresponding character at compile time. Ordinary strings that happen to contain backwards slashes are not transformed.

For example, the strings notEscaped and notEscaped2 below are not transformed to a newline character, but will stay as two different characters ('\' and 'n').

string escaped = "\n";
string notEscaped = "\\" + "n";
string notEscaped2 = "\\n";

Console.WriteLine(escaped.Length); // 1
Console.WriteLine(notEscaped.Length); // 2            
Console.WriteLine(notEscaped2.Length); // 2

Related Examples