C# escape for regular string literals
The C# escape tool turns plain text into the body of a regular C# string literal — the kind written as "…". It is useful for connection strings, Windows paths, JSON snippets and test data that you want to paste into .NET code without hunting for every backslash.
Escape sequences used
| Character | C# escape |
|---|---|
| Backslash, double quote, single quote | \\, \", \' |
| NUL, alert (bell), backspace | \0, \a, \b |
| Form feed, line feed, carriage return | \f, \n, \r |
| Tab, vertical tab | \t, \v |
| Other control and all non-ASCII characters | \uXXXX |
The tool never emits C#’s \x escape. That escape takes a variable number of hex digits (one to four), so "\x41BC" is a single character U+41BC rather than “ABC”. Using fixed-length \u escapes avoids that trap.
Example
Input:
Server="db01";Path=C:\Apps
Note: caféOutput:
Server=\"db01\";Path=C:\\Apps\nNote: caf\u00E9
Verbatim and raw strings
C# offers alternatives that may suit some text better than escaping:
- Verbatim strings
@"C:\Apps\Data"— backslashes are literal and the only escape is""for a quote. Great for paths and regular expressions. - Raw string literals (C# 11)
"""…"""— no escaping at all, ideal for JSON and XML blocks. - Interpolated strings
$"…"— escape braces as{{and}}; this tool does not do that for you.
Emoji, char literals and generated code
Characters above U+FFFF, such as emoji, are written as two \u escapes because .NET strings are UTF-16: 🚀 becomes \uD83D\uDE80. C# also accepts the eight-digit form \U0001F680, which the .NET Unescape tool decodes as well.
The same escapes work in char literals, where the single quote is the one that matters: '\''. That is why the tool escapes both quote types — the output can go into either kind of literal. When you generate C# source programmatically, Roslyn’s SymbolDisplay.FormatLiteral() produces an escaped literal for you; this tool is for the times you paste text by hand.
Non-ASCII letters such as é or ü are escaped as well. That keeps source files pure ASCII, so they compile the same way regardless of the editor’s encoding settings — useful for shared repositories and older projects that are not saved as UTF-8.
Decode existing literals with .NET Unescape and format whole files with the C# Formatter. The escaping happens in your browser.