Ways to print escape characters in C#

The following are the escape characters in C# and the display column suggests how to use and print them in C# −

Escape CharacterDescriptionPatternDisplay
\aMatches a bell character, \u0007.\a"\u0007" in "Warning!" + '\u0007'
\bIn a character class, matches a backspace, \u0008.[\b]{3,}"\b\b\b\b" in "\b\b\b\b"
\tMatches a tab, \u0009.(\w+)\t"Name\t", "Addr\t" in "Name\tAddr\t"
\rMatches a carriage return, \u000D. (\r is not equivalent to the newline character,
Hello" in "\r\Hello
\vMatches a vertical tab, \u000B.[\v]{2,}"\v\v\v" in "\v\v\v"
\fMatches a form feed, \u000C.[\f]{2,}"\f\f\f" in "\f\f\f"

Matches a new line, \u000A.\r
Hello" in "\r\Hello
\eMatches an escape, \u001B.\e"\x001B" in "\x001B"

Uses octal representation to specify a character (nnn consists of up to three digits).\w\040\w"a b", "c d" in "a bc d"
\x nnUses hexadecimal representation to specify a character (nn consists of exactly two digits).exactly two digits).\w\x20\w\w\x20\w
\c X\c xMatches the ASCII control character that is specified by X or x, where X or x is the letter of the control character.\cC"\x0003" in "\x0003" (Ctrl-C)
\u nnnnMatches a Unicode character by using hexadecimal representation (exactly four digits, as represented by nnnn).\w\u0020\w"a b", "c d" in "a bc d"
\When followed by a character that is not recognized as an escaped character, matches that character.\d+[\+-x\*]\d+\d+[\+-x\*\d+"2+2" and "3*9" in "(2+2) * 3*9"

The following is an example showing how to use some of the escape characters in C# −


 Live Demo

using System;
using System.Collections.Generic;

class Demo {
   static void Main() {

      Console.WriteLine("Warning!" + '\u0007');
      Console.WriteLine("Demo Text \t Demo Text");
      Console.WriteLine("This is it!
This is on the next line!");    } }


Demo Text Demo Text
This is it!
This is on the next line!

Updated on: 20-Jun-2020


Kickstart Your Career

Get certified by completing the course

Get Started