Why escape text in C#?
Escaping text in C# is necessary when you want to include special characters in a string. C# supports different types of string literals, each with its own escaping rules.
Examples of C# escaping
Original text
Hello "World"
Line with 'single' quotes
Tab and newline
characters
Backslash \ and other special chars
Curly braces { and } for interpolationEscaped text (regular string)
\bHello\b \"\bWorld\b\"\\bnLine\b \bwith\b '\bsingle\b' \bquotes\b\\bnTab\b\\btand\b \bnewline\b\\bncharacters\b\\bnBackslash\b \\ \band\b \bother\b \bspecial\b \bchars\b\\bnCurly\b \bbraces\b { \band\b } \bfor\b \binterpolation\bC# String Types
1. Regular strings
Similar to Java/JavaScript with escaping for quotes, backslashes, and control characters.
- Double quote:
\" - Backslash:
\\ - Newline:
\n - Tab:
\t - Carriage return:
\r - Form feed:
\f - Backspace:
\b - Null character:
\0
string regularString = "Hello \"World\"\nNew line";2. Verbatim strings (@"...")
Verbatim strings preserve all whitespace and newlines. Only double quotes need to be escaped (as "").
string verbatimString = @"Hello ""World"" New line without \n";3. Interpolated strings ($"...")
Interpolated strings allow embedding expressions within curly braces. They need escaping like regular strings, plus curly braces need to be doubled.
string interpolatedString = $"Hello {{name}}\nValue: {{value}}";This tool is useful for preparing text to be used in C# code, ensuring that special characters are properly escaped according to the string type you're using.
Tip:
When working with large blocks of text in C#, consider using verbatim strings (@"...") to preserve formatting and reduce the need for escaping newlines and backslashes. For strings with variable interpolation, use interpolated strings ($"...") but remember to escape curly braces by doubling them.
Related Tools
- Unescape C# - Convert escaped C# strings back to their original form
- Escape Java - Escape text for use in Java string literals
- Escape JavaScript - Escape text for use in JavaScript
- Escape JSON - Escape text for use in JSON
- Escape XML - Escape text for use in XML
All Tools
See all available tools