Escape Characters

In this chapter you will learn:

  1. How to escape characters in String
  2. Character escape sequence: \0
  3. Escape sequences in strings: embed quotes
  4. Escape sequences in strings: \t (tab)
  5. Escape sequences in strings: \n (new line)

Escape strings

Escape Sequence Character NameUnicode Encoding
\' Single quote 0x0027
\" Double quote 0x0022
\\ Backslash0x005C
\0 Null0x0000
\a Alert 0x0007
\b Backspace0x0008
\f Form feed0x000C
\n newline 0x000A
\r Carriage return 0x000D
\t Horizontal tab0x0009
\v Vertical tab 0x000B
\uxxxx Unicode character in hex\u0029
using System;//from   j a v a  2  s. c  om


public class MainClass
{
    public static void Main()
    {
        Console.WriteLine("Single quote \\': {0} ({1})", '\'', (int)'\'');
        Console.WriteLine("Double quote \\\": {0} ({1})", '\"', (int)'\"');
        Console.WriteLine("Backslash \\\\: {0} ({1})", '\\', (int)'\\');
        Console.WriteLine("Unicode null \\0: {0} ({1})", '\0', (int)'\0');
        Console.WriteLine("Alert \\a: {0} ({1})", '\a', (int)'\a');
        Console.WriteLine("Backspace \\b: {0} ({1})", '\b', (int)'\b');
        Console.WriteLine("Horizontal tab \\t: {0} ({1})", '\t', (int)'\t');
        Console.WriteLine("Newline \\r: {0} ({1})", '\r', (int)'\r');
        Console.WriteLine("Vertical quote \\v: {0} ({1})", '\v', (int)'\v');
        Console.WriteLine("Form feed \\f: {0} ({1})", '\f', (int)'\f');
        Console.WriteLine("Carriage return \\r: {0} ({1})", '\r', (int)'\r');
    }

}

The code above generates the following result.

Character escape sequence: \0

using System;//j ava  2  s .  co m

class MainClass
{
  static void Main(string[] args)
  {
    char MyChar = '\0';
    
    Console.WriteLine(MyChar);
  }
}

Escape sequences in strings: embed quotes

using System; //from  j  ava  2s  .  c o  m
 
class Example {    
  public static void Main() {    
 
    Console.WriteLine("\"Why?\", he asked."); 
  }
}

The code above generates the following result.

Escape sequences in strings: \t (tab)

using System; //j  a v  a2s. co m
 
class Example {    
  public static void Main() {    

    Console.WriteLine("One\tTwo\tThree"); 
    Console.WriteLine("Four\tFive\tSix"); 
  }
}

The code above generates the following result.

Escape sequences in strings: \n (new line)

using System; /* j  av a  2  s .c o m*/
 
class Example {    
  public static void Main() {    
    Console.WriteLine("Line One\nLine Two\nLine Three"); 
  }    
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Verbatim string literal
  2. Quotation in verbatim string
  3. multiple line string
Home » C# Tutorial » String
string
String creation
Char in string
Compare strings
String equality
String concatanation
String copy
String Join
String split
String Search for Index
String contains
String start with
String insert
String case
Replacing substring
Remove from a string
Substring
Escape Characters
String verbatim
String padding
Switch on String
String trim
String intern
String normalization
Empty String