Week 128 — What are text blocks and how can they be used?
Question of the Week #128
What are text blocks and how can they be used?
5 Replies
String
literals are enclosed with double quotes.
These "normal" String
literals can only span a single line. If a String
consisting of multiple lines is needed, it is possible to use the \n
escape sequence:
An alternative to this is to use text blocks which have been introduced in JDK 15 (first preview in JDK 13). A text block starts and ends with """
and allows line breaks in between:
To ensure leading and trailing space is properly handled, Java differentiates between essential whitespace and incidential whitespace. Incential whitespace is all leading whitespace in the text block that is present in all lines. If two lines in the same text block have a different amount of leading whitespace, only the minimum amount is considered incidential whitespace. Incidential whitespace is not included in the resulting String
. On the other hand, all other (leading) whitespace is considered essential whitespace and included in the String
.
The above code outputs the following text:
The two spaces that are present in all 3 lines are considered incidential whitespace and not included. The two remaining spaces before World
and the one remaining whitespace before :)
are considered essential whitespace and hence included.Text blocks can also include line breaks that are not included in the resulting
String
. This can be done by adding a \
at the end of the line before the line break.
📖 Sample answer from dan1st
Text blocks are a Java language feature that allows to use multiline string embedded in your source code. To use this feature you just open three double quotes and a new line to start the block, put the multiline text in the next line and close three double quotes to end the text block.
Example:
String myTextBlock = """
This is the first line.
This is the second line.""";
Submission from maicosmaniotto
Text blocks were introduced as a preview feature in Java 13, as a feature designed to make working with multiline string literals easier, and they've became a standard feature since Java 15.
The primary goal of text blocks is to simplify the creation of strings that consists of multiple lines and escape sequences, such as snippets of JSON, XML, HTML, or SQL, by reducing string concatenations and escaping in general.
Syntax:
A text block starts with three double-quote characters
"""
followed immediately by a newline. The content of the string follows on subsequent lines, and the text block is closed by another three double-quote characters """
.
Example:
HTML:
Traditional Muti-line String:
With Text Blocks:
JSON Example:
Traditional Mutli-line String:
With Text Blocks:
Key characteristics of text-blocks:
- Indentation Management: The compiler looks at all the lines in your text block. It figures out the smallest amount of space that every non-empty line starts with. This "common" leading whitespace is then removed from every line. It also gets rid of any spaces at the very end of each line.
- Trailing Whitespace: Trailing whitespace on each line is generally removed. To preserve trailing whitespace, you can use the escape sequence
\s
.
- Line Terminators: Line breaks within the text block are normalized to \n
in the resulting string.
- Escaping: Most escape sequences like \n
or \t
are not needed. However, to include """
within the text block itself, you can escape one of the quotes, for example, \"""
.⭐ Submission from daysling