User Rating 0.0
Total Usage 0 times
0 chars
0 chars
Is this tool helpful?

Your feedback helps us improve.

About

C# string literals require specific escape sequences for characters that conflict with the language syntax or represent non-printable control codes. A misplaced backslash or unescaped quotation mark causes compile-time error CS1010 or silent data corruption when writing to files and databases. This tool performs bidirectional conversion between raw text and C# escaped string content, handling all standard sequences defined in ECMA-334 including \uXXXX Unicode escapes for characters above U+007E. It processes \n, \t, \r, \0, \a, \b, \f, \v, \\, \", and \' in both directions.

The unescape parser uses a single-pass finite state machine to correctly resolve chained sequences without double-processing. Note: this tool handles regular string literals, not verbatim strings (prefixed with @) which use different escaping rules. Pro tip: when embedding JSON inside C# strings, escape the entire payload first, then verify the output compiles by checking for unmatched backslashes.

csharp escape unescape string code utility c# escape sequences unicode escape

Formulas

The escape transformation applies a deterministic character mapping function E over each character c in the input string S:

E(S) = ni=0 map(ci)

Where map(c) is defined as a piecewise function:

{
\\ if c = \\n if c = LF (U+000A)\t if c = HT (U+0009)\uXXXX if charCode(c) > 126c otherwise

The unescape operation U is the inverse: it scans for \ followed by a recognized suffix character and replaces the two-character sequence (or six-character \uXXXX) with the corresponding literal character. Order of processing matters: \\ must be resolved before single \ to prevent cascade errors.

Where S = input string, ci = character at index i, n = length of string, charCode = UTF-16 code unit value.

Reference Data

Escape SequenceCharacterUnicode Code PointDescription
\''U+0027Single quotation mark
\""U+0022Double quotation mark
\\\U+005CBackslash
\0NULU+0000Null character
\aBELU+0007Alert (bell)
\bBSU+0008Backspace
\fFFU+000CForm feed
\nLFU+000ANew line (line feed)
\rCRU+000DCarriage return
\tHTU+0009Horizontal tab
\vVTU+000BVertical tab
\uXXXXUnicodeU+0000 to U+FFFFUnicode escape (4 hex digits)
\UXXXXXXXXUnicodeU+00000000 to U+0010FFFFUnicode escape (8 hex digits, surrogate pairs)
\xN[N][N][N]UnicodeU+0 to U+FFFFVariable-length hex escape (1-4 digits)

Frequently Asked Questions

Characters above U+FFFF (such as emoji or rare CJK ideographs) are represented in C# as surrogate pairs. This tool encodes each surrogate half as a separate \uXXXX sequence, matching the behavior of the C# compiler when processing regular string literals. For example, the character U+1F600 becomes \uD83D\uDE00.
The \uXXXX form requires exactly 4 hexadecimal digits and covers U+0000 to U+FFFF. The \UXXXXXXXX form requires exactly 8 hex digits and can address the full Unicode range up to U+10FFFF. The \xN form accepts 1 to 4 hex digits but is ambiguous when followed by valid hex characters. This tool uses \u for escaping as it is the safest and most portable option.
The \x escape is greedy: it consumes as many hex digits as available (up to 4). For instance, \x41BC is a single character U+41BC, not the letter A followed by BC. This tool parses \x sequences by consuming 1 to 4 trailing hex digits, matching C# compiler behavior. Avoid \x in production code; prefer \u for clarity.
Escaping always increases or maintains string length. Each special character expands: a single backslash (1 char) becomes \\ (2 chars), a newline (1 char) becomes \n (2 chars), and a Unicode character (1 char) becomes \uXXXX (6 chars). The inverse is true for unescaping. The character count displayed by this tool reflects the actual output length.
Verbatim strings use only one escape rule: a double quotation mark is represented as "". Backslash has no special meaning. This tool processes regular string literals only. For verbatim strings, simply double any quotation marks manually. Mixing the two forms in the same codebase is valid but requires attention to context.
Not directly. JSON and C# share some escape sequences (\n, \t, \") but differ on others. C# supports \0, \a, \v which JSON does not. After C# escaping, you must additionally escape characters that JSON requires (such as forward slash in some implementations). Process each encoding layer separately to avoid corruption.
This tool preserves unrecognized escape sequences verbatim. If the input contains \q (which is not a valid C# escape), the output retains the literal characters \q. The C# compiler would reject this with error CS1009. The tool displays a warning toast when unrecognized sequences are encountered.