String Builder

Turn a block of plain lines into a ready to paste string literal for the language you are working in.

Lines
Source code
Runs locally in your browser

About the String Builder

Embedding a multi line query or template into source code is fiddly work that nobody enjoys doing by hand. Paste the lines here, choose a language, and each one comes back quoted, escaped and joined in the idiom that language uses: + for JavaScript and Go, a leading + on continuation lines for Java and C#, a dot for PHP, implicit adjacency for Python, and the double pipe operator for SQL.

Escaping follows the target rather than one generic rule. Backslashes and the active quote character are escaped for the C family and Python, while SQL takes the different route of doubling a single quote, which is the only correct way to embed an apostrophe in a standard SQL literal. The variable name box is validated so a name starting with a digit is refused before it reaches your editor.

The array style is often the better choice for anything long, because a list of quoted strings joined on a newline stays readable in code review and survives reordering. The quoted lines only style skips the declaration when you are pasting into an existing expression. Ticking the newline option appends \n to each line so the rebuilt string keeps its shape, which matters for a SQL statement that must stay formatted in the logs. Going the other way, JSON to String handles whole documents, and JavaScript Escape escapes text without building a declaration.

How to use

  1. Paste the lines you want to embed in code.
  2. Choose the Target language and a Build style.
  3. Set the variable name, and untick the newline option if the joined string should be one continuous line.
  4. Copy the generated literal into your source file.

Common questions

Why does SQL double the apostrophes instead of adding backslashes?
Standard SQL escapes a quote by repeating it. A backslash would be stored literally in most databases.
Which style should I use for a long query?
Array joined on newline. It stays readable, and adding or moving a line does not disturb the surrounding operators.
Are blank lines kept?
No. Empty lines are dropped so the literal has no stray empty strings in it.
Can I use single quotes in JavaScript?
Yes. Set the quote character to single and any apostrophe in your text is escaped instead of the double quotes.