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 Character Description Pattern Display
\a Matches a bell character, \u0007. \a "\u0007" in "Warning!" + '\u0007'
\b In a character class, matches a backspace, \u0008. [\b]{3,} "\b\b\b\b" in "\b\b\b\b"
\t Matches a tab, \u0009. (\w+)\t "Name\t", "Addr\t" in "Name\tAddr\t"
\r Matches a carriage return, \u000D. (\r is not equivalent to the newline character,
.)
\r
(\w+)
"\r\nHello" in "\r\Hello\nWorld."
\v Matches a vertical tab, \u000B. [\v]{2,} "\v\v\v" in "\v\v\v"
\f Matches a form feed, \u000C. [\f]{2,} "\f\f\f" in "\f\f\f"

Matches a new line, \u000A. \r
(\w+)
"\r\nHello" in "\r\Hello\nWorld."
\e Matches an escape, \u001B. \e "\x001B" in "\x001B"
\nnn 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 nn Uses hexadecimal representation to specify a character (nn consists of exactly two digits). \w\x20\w \w\x20\w
\c X\c x Matches 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 nnnn Matches 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"

Common Escape Characters

The most commonly used escape characters in C# programming are
for new lines, \t for tabs, " for double quotes, and \ for backslashes −

using System;

class Demo {
    static void Main() {
        // Tab character
        Console.WriteLine("Name\tAge\tCity");
        Console.WriteLine("John\t25\tNew York");
        
        // New line character
        Console.WriteLine("First line\nSecond line");
        
        // Quote and backslash
        Console.WriteLine("He said, "Hello World!"");
        Console.WriteLine("File path: C:\Documents\file.txt");
    }
}

The output of the above code is −

Name    Age     City
John    25      New York
First line
Second line
He said, "Hello World!"
File path: C:\Documents\file.txt

Using Unicode Escape Sequences

Unicode escape sequences allow you to represent any Unicode character using \u followed by four hexadecimal digits −

using System;

class UnicodeDemo {
    static void Main() {
        // Unicode characters
        Console.WriteLine("\u0048\u0065\u006C\u006C\u006F"); // Hello
        Console.WriteLine("Copyright symbol: \u00A9");
        Console.WriteLine("Heart symbol: \u2665");
        Console.WriteLine("Degree symbol: 98.6\u00B0F");
    }
}

The output of the above code is −

Hello
Copyright symbol: ©
Heart symbol: ?
Degree symbol: 98.6°F

Verbatim Strings vs Escape Characters

C# provides verbatim strings (prefixed with @) as an alternative to escape characters, particularly useful for file paths and multi-line strings −

using System;

class VerbatimDemo {
    static void Main() {
        // Using escape characters
        string path1 = "C:\Program Files\MyApp\config.txt";
        
        // Using verbatim string
        string path2 = @"C:\Program Files\MyApp\config.txt";
        
        Console.WriteLine("With escapes: " + path1);
        Console.WriteLine("Verbatim: " + path2);
        
        // Multi-line verbatim string
        string multiLine = @"Line 1
Line 2
Line 3";
        Console.WriteLine("\nMulti-line string:");
        Console.WriteLine(multiLine);
    }
}

The output of the above code is −

With escapes: C:\Program Files\MyApp\config.txt
Verbatim: C:\Program Files\MyApp\config.txt

Multi-line string:
Line 1
Line 2
Line 3

Comparison of Methods

Method Use Case Example
Escape Characters Single-line strings with special characters "Hello\nWorld"
Verbatim Strings File paths, multi-line text @"C:\folder\file.txt"
Unicode Sequences Special symbols and international characters "\u00A9 2023"

Conclusion

Escape characters in C# provide a way to include special characters in strings that would otherwise be difficult to represent. The most common ones are
, \t, ", and \, while Unicode sequences allow representation of any character. Verbatim strings offer an alternative approach for complex paths and multi-line text.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements