FormatterHQ

.NET (C#) String Escape

Convert text into a valid C# regular string literal. Handles quotes, backslashes, all of C#’s simple escapes and writes non-ASCII characters as \uXXXX.

Loading tool…
Ready.

How to use the .NET (C#) Escape

  1. Type or paste into the Raw text box, or click Sample. You can also drop a text file on either box, or load one into the Escaped .NET (C#) box with its Open file button.
  2. The Escaped .NET (C#) box updates live as you type. It works both ways — edit the Escaped .NET (C#) side to convert back, or click ⇄ Swap to exchange the sides.
  3. Use Copy or Download on either box to take the result, or Clear to start again.

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

CharacterC# 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.

Frequently asked questions

How do I escape a backslash in C#?
Write it twice (\\) in a regular string, or use a verbatim string with the @ prefix, where a single backslash is literal: @"C:\Temp".
How do I escape a double quote in a verbatim string?
In an @"…" string, write two double quotes (""). Backslash escapes do not work in verbatim strings.
Why does the tool avoid \x escapes?
C#’s \x accepts one to four hex digits, so the following text can accidentally become part of the escape. \u always takes exactly four digits and is unambiguous.
Is this the same as Regex.Escape?
No. Regex.Escape escapes regular expression metacharacters like . * and ( ). This tool escapes characters for a C# string literal. You may need both when writing a regex pattern in code.

Last updated October 3, 2026

Related tools