Format Code Now
/

C# Escape and Unescape

ED-43

Paste text and click C# escape to get the backslash escapes a C# string literal needs, or click C# unescape to turn an escaped string back into plain text.

Runs in your browser. Nothing is uploaded.

Text or C# string

Result

How to use it

  1. Paste your text, or a C# string, into the input box.
  2. Tick the options you need: escape non-ASCII as \uXXXX, escape single quotes, or add surrounding quotes.
  3. Click C# escape or C# unescape for a regular string. For an @"..." string use Escape for @"..." or Unescape @"...".
  4. Copy the result into your code.

Regular strings

C# escape writes \\ for a backslash, \" for a double quote, and \0, \a, \b, \t, \n, \v, \f and \r for the matching control characters. Other control characters become \u00XX. Characters such as é are kept unless you tick the non-ASCII option. Unescape also reads \x with one to four hex digits, \uXXXX and \UXXXXXXXX.

Verbatim strings

In a @"..." string a backslash is just a backslash and a line break stays a line break. The only escape is doubling a double quote. So the verbatim actions change nothing except quotes, which suits file paths. Interpolated strings with $ and raw string literals are not handled by this tool, and nothing here compiles or runs C#.

Error messages and what to do

MessageWhat it meansFix
Unknown escapeThe text has a backslash followed by a character C# does not define as an escape.Double the backslash, or use one of the listed escapes.
A single double quoteA verbatim string has a lone double quote, which would end it.Write each double quote as two double quotes.

Questions people ask

How do I escape a string in C#?

Put a backslash before backslashes and double quotes, and use \n for line breaks and \t for tabs. This page does it for you: paste the text, click C# escape, and add surrounding quotes with the option if you want a ready literal.

How do I escape double quotes in a C# string?

In a regular string write \" for each double quote. In a verbatim @ string write two double quotes instead. Use C# escape for the first case and Escape for @"..." for the second.

What is a verbatim string in C#?

A verbatim string starts with @" and keeps backslashes and line breaks exactly as typed, which makes Windows paths easier to read. The only thing you must change is each double quote, which is written twice.

Does C# unescape handle unicode escapes?

Yes. It reads \uXXXX with four hex digits and \UXXXXXXXX with eight, and \x with one to four hex digits. A surrogate written with \U, or a value above U+10FFFF, is reported as an error.

Should I escape single quotes in C#?

Only inside a char literal such as '\''. A double-quoted string does not need it, so the option is off by default. Tick Also escape single quotes if you are building a char literal or want the extra escape.

About this tool

C# escape writes backslash sequences for backslash, double quote, NUL, bell, backspace, tab, line feed, vertical tab, form feed and carriage return, and \u00XX for other control characters. The verbatim actions only double each double quote, because a @"..." literal keeps backslashes and line breaks as typed. Unescape reads the regular escapes, including \x with 1 to 4 hex digits, \uXXXX and \UXXXXXXXX, and removes one pair of outer quotes if present. It does not compile or run C#, and it does not handle interpolated ($"...") or raw string literals. Nothing is uploaded.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.