Text block """
A multi-line string literal in triple quotes.
Available since: Java 15
Syntax
""" ... """Examples
A text block with JSON — no need to escape the quotes inside:
void main() {
String json = """
{
"name": "Anna",
"age": 30
}
""";
IO.print(json);
}Output:
{
"name": "Anna",
"age": 30
}The common indent is removed; the last line ends with a line break if the closing quotes are on a new line:
void main() {
String a = """
hello
""";
String b = """
hello""";
IO.println(a.length() + " " + b.length());
}Output:
6 5Notes
The opening """ must be followed by a line break. The indent is taken from the leftmost line, including the line with the closing quotes. Trailing spaces are removed; \ at the end of a line joins it with the next one, and \s keeps a space.
See also
- String.formatted() — Uses the string itself as a template and fills it in — a shorter form of String.format().
- String.lines() — Splits a string at line breaks and returns the lines as a stream.
- String.strip() — Removes any Unicode whitespace from both ends of a string.
Related recipes
In the tutorial
- Text blocks and formatting — Multi-line text blocks in Java, formatting strings with formatted, String.format and printf, the %s, %d and %f specifiers and field width.