FormatterHQ

String Builder Code Generator

Turn multi-line text such as HTML, SQL or a template into ready-to-paste StringBuilder or string-building code for Java, C#, JavaScript, Python, PHP or Go.

Input TEXT
Output TEXT
Ready.

How to use the String Builder

  1. Paste the multi-line text you want to embed in code.
  2. Choose the target language and, if you like, a variable name.
  3. Decide whether a newline should be added after each line.
  4. Copy the generated code into your project.

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

LanguageGenerated patternNewline handling
JavaStringBuilder with append().append("\n") after each line
C#StringBuilder with AppendLine() or Append()AppendLine adds Environment.NewLine, which is \r\n on Windows
JavaScriptArray with push(), then join()Joined with \n between lines
PythonList with append(), then "".join()Joined with \n between lines
PHPString with .= concatenation. "\n" after each line
Gostrings.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.

Frequently asked questions

How are double quotes escaped in the generated code?
Every double quote becomes \" and every backslash becomes \\, the same escapes Java, C#, JavaScript, Python, PHP and Go all accept in double-quoted strings. Tabs become \t and other control characters become \u escapes.
Why does the PHP output contain \$?
PHP expands variables inside double-quoted strings, so "$5" or "$name" would be interpreted. Escaping the dollar sign keeps it as a literal character.
Does C# AppendLine add \n or \r\n?
AppendLine appends Environment.NewLine, which is \r\n on Windows and \n on Linux and macOS. Turn the newline option off and add your own "\n" if you need identical output everywhere.
Can I change the variable name?
Yes. Enter any valid identifier and it is used for the builder, list or string throughout the generated code. The default is sb.

Last updated September 26, 2026

Related tools

JSON Multiline String Converter Convert multi-line text into a valid JSON string with \n escapes, a JSON array of lines, or concatenated string literals. JSON to String Converter Convert JSON to a single escaped string you can paste into code, databases or other JSON documents. Choose double, single or no quotes. Random String Generator Generate random strings of any length from letters, digits, hex, URL-safe or custom character sets. Reverse String Reverse text by characters, words or lines, or flip the letters of each word individually. String Byte Size Counter Count how many bytes a string uses in UTF-8, UTF-16 and UTF-32, plus characters, code points and Base64/URL-encoded size. String to JSON Converter Convert an escaped JSON string back into formatted JSON. Handles quoted literals, backslash-escaped text and double-encoded JSON. String Utility Inspect a string: length, UTF-8 byte size, words, lines, plus uppercase, lowercase, reversed and trimmed versions. Text Cleaner Clean messy text: remove invisible characters, straighten smart quotes, collapse spaces and strip HTML. Text Formatter Clean up messy text: trim and collapse spaces, fix punctuation spacing, capitalize sentences, normalize quotes, blank lines and line endings. Text Minifier Compress plain text by collapsing whitespace and removing blank lines. Text Repeater Repeat a text or emoji up to 10,000 times, separated by new lines, spaces, commas or nothing. Text Replacer Find and replace text online with case-sensitive, whole-word and regular expression options.