String Builder
TX-55Paste several lines of text and get a string variable for your language, with each line as its own piece, joined the way that language expects and with quotes and backslashes escaped.
Runs in your browser. Nothing is uploaded.
Optional first line: -- name: query. Then the lines to turn into a string
Code
How to use it
- Optionally write -- name: query on the first line to set the variable name. The default is text.
- Paste the lines you want as a string under it.
- Pick the language, and what goes between lines: a line break, a space or nothing.
- Press Build string, then copy the code into your project. Untick Include the variable declaration to get only the expression.
How each language is written
JavaScript, Java, C# and Go join the pieces with +, PHP uses a dot, Python and C put the pieces next to each other and rely on the language to join them, and Ruby joins with +. JavaScript also has a template literal option that keeps your line breaks as real line breaks. When you choose a line break between lines, each piece ends with the language's escape for a line break, so the final string holds the same lines as your text.
What is escaped
Backslashes, double quotes, tabs and control characters are escaped for the language you pick. In PHP a dollar sign is escaped so it is not read as a variable, in Ruby a hash before a brace is escaped, and in a JavaScript template literal backticks and the dollar-brace pair are escaped. Characters outside ASCII are written as they are, so save the file as UTF-8. Single quotes in your text need no escaping because the pieces use double quotes.
What it does not do
The tool writes the code but does not run or check it. It does not produce raw strings, heredocs or the triple-quoted forms some languages have, and it uses a plain name for the variable, so you may need to rename it to fit your style. For one-off escaping of a single string without building variable code, use the escape tools for JavaScript, Java, C# or JSON.
Error messages and what to do
| Message | What it means | Fix |
|---|---|---|
Paste the lines to turn into a string. | There is no text under the optional name line. | Paste the lines you want to turn into a string. |
Questions people ask
How do I turn multiline text into a JavaScript string?
Paste the lines, choose JavaScript and press Build string. You get a const with each line as a quoted piece joined by plus signs, and a line break escape at the end of every line but the last.
How do I build a multiline SQL string in code?
Paste the query lines, optionally name the variable on the first line with -- name: query, choose your language and press Build string. Quotes in the query are escaped for you.
Can I get a JavaScript template literal instead?
Yes, choose JavaScript (template literal) as the language. Your line breaks stay as real line breaks inside backticks, and backticks, backslashes and the dollar-brace pair are escaped so the string stays valid.
How do I set the variable name?
Write -- name: followed by the name on the first line, for example -- name: query. The name may use letters, digits and underscores and must not start with a digit.
Which languages are supported?
JavaScript (concatenation and template literal), Python, Java, C#, PHP, Go, Ruby and C. Each uses its own joining style and its own escapes for control characters.
About this tool
Each input line becomes one string piece, and the pieces are joined the way the chosen language expects: + in JavaScript, Java, C# and Go, . in PHP, adjacent literals in Python and C, and a template literal for the backtick option. The first line can set the variable name with -- name:; without it the name is text. Quotes, backslashes, tabs and control characters are escaped for that language, $ is escaped in PHP, and ${ and backticks in a template literal. Characters outside ASCII are written as they are, so save the file as UTF-8. The tool writes the code but does not run or check it, and it does not handle language-specific multi-line or raw string forms. Nothing is uploaded.