String Escape and Unescape
Choose a language, paste your text and get a string literal that is correct for it. Switch to unescape to turn an escaped string back into the plain text it stands for.
Worked examples
JavaScript, plain ASCII
Café "20%" \ 😀
"Caf\u00E9 \"20%\" \\ \uD83D\uDE00"
The emoji is written as a surrogate pair.
SQL
O'Brien's
'O''Brien''s'
Each single quote is doubled.
Shell
it's here
'it'\''s here'
The apostrophe closes the quotes, is escaped, and reopens them.
About String Escape and Unescape
A string that is fine on screen can break your code. A double quote ends a JavaScript string early, a single quote ruins a SQL statement, a backslash changes the meaning of the next character, and a line break is not allowed in many literals. Escaping adds the characters that let the string sit between quotes in a given language, and the rules differ from one language to the next.
Choose JSON, JavaScript, Python, Java, C#, a C# verbatim string, SQL, MySQL, CSV or a shell string and paste your text. You get a literal that is correct for that language, with the surrounding quotes if you want them. Unescape works the other way, turning backslash sequences such as \n, \t, \xE9, \u00E9 and \u{1F600} back into the characters they stand for.
How to use String Escape and Unescape
- Pick Escape or Unescape, and the language or format.
- Paste the text. Choose single or double quotes where the language allows both.
- Decide whether to keep the surrounding quotes and whether to write non-ASCII characters as \u escapes.
- Copy the result into your code.
Common uses
- Putting a block of text into a JSON string or a source file
- Building a SQL statement by hand for a quick fix
- Reading a log line full of \n and \u00e9 sequences
- Quoting a value for a shell command or a CSV field
Frequently asked questions
What is the difference between standard SQL and MySQL escaping?
Standard SQL escapes a single quote by doubling it. MySQL also treats the backslash as an escape character by default, so it uses \' and \\. Pick the one that matches your server.
How is a CSV field escaped?
Double quotes inside the value are doubled and the whole value is wrapped in quotes when it contains a comma, a quote or a line break.
Why would I write non-ASCII characters as \u escapes?
It keeps the file plain ASCII, which is useful for older tools and for files that pass through systems with a different encoding. Characters outside the basic range are written as a surrogate pair, or as \U in Python.
What is a C# verbatim string?
A string written as @"..." in which the backslash means nothing special and a double quote is written twice. It is handy for file paths.
Does unescape handle every form?
It reads \n \r \t \b \f \v, \xHH, \uHHHH, \u{...}, \U with eight digits for Python, doubled quotes for SQL, CSV and verbatim strings, and the shell form for an apostrophe. Invalid JSON escapes are reported with their position.