Generate string builder code from text
This string builder generator converts a block of text into code that rebuilds it line by line. Each line becomes an escaped string literal appended to a builder, list or string variable. Paste an HTML snippet, SQL statement or email template and you get code you can drop straight into Java, C#, JavaScript, Python, PHP or Go.
Example (Java)
<h1>Hello "World"</h1>
Price: $5
StringBuilder sb = new StringBuilder();
sb.append("<h1>Hello \"World\"</h1>").append("\n");
sb.append("Price: $5").append("\n");
String result = sb.toString();
What each language gets
| Language | Generated pattern | Newline handling |
|---|---|---|
| Java | StringBuilder with append() | .append("\n") after each line |
| C# | StringBuilder with AppendLine() or Append() | AppendLine adds Environment.NewLine, which is \r\n on Windows |
| JavaScript | Array with push(), then join() | Joined with \n between lines |
| Python | List with append(), then "".join() | Joined with \n between lines |
| PHP | String with .= concatenation | . "\n" after each line |
| Go | strings.Builder with WriteString() | + "\n" after each line |
There is a subtle difference: the Java, C#, PHP and Go versions add a newline after every line, including the last, while the join-based JavaScript and Python versions only place one between lines. Switch the newline option off to concatenate lines with nothing in between. For C# add using System.Text;, and for Go import "strings". Java's StringBuilder needs no import.
How escaping works
Each line is written as a double-quoted literal with JSON-style escapes: \" for a double quote, \\ for a backslash and \t for a tab, which all six languages understand. Rare control characters become \uXXXX escapes; PHP does not interpret that form (it uses \u{...}), so remove such characters first when targeting PHP. Non-ASCII characters such as é or ✓ are kept as they are, so save your source file as UTF-8.
PHP needs one extra step, because double-quoted PHP strings expand variables. Every $ is therefore written as \$, and Price: $5 becomes "Price: \$5". The generated PHP code has no separate result line; the variable itself holds the finished string.
Builder code vs. multi-line literals
Modern languages can hold multi-line text directly: Java text blocks (Java 15+), C# raw string literals (C# 11), Python triple-quoted strings, JavaScript template literals and Go raw strings in backquotes. For fixed text these are often simpler. Builder code is still useful when you plan to add conditional lines or loops, when you target older language versions, or when your team's code style expects it.
If you only need a single escaped string rather than builder code, use Java escape, .NET (C#) escape or JSON escape.
Privacy
The processing runs entirely in your browser. The text you enter is not uploaded to a server.