Using Escape Sequences in Text Blocks
We do not need to end each text line with the \n escape sequence, as the text block retains the line terminators entered directly into the text. However, using the \n escape sequence will be interpreted as literally inserting a newline character in the text. The query sql2 below is equivalent to the query sql1 above, and will result in the same string. (For escape sequences, see Table 2.10, p. 38.)
String sql2 = “””
SELECT *
FROM Programmers\nWHERE Language = ‘Java’;
“””;
The string literal resulting from entering the text block in the jshell tool:
sql2 ==> “SELECT *\nFROM Programmers\nWHERE Language = ‘Java’;\n”
However, ending a line explicitly in the text block with a newline character will result in an empty line being inserted into the resulting string, as can be seen in the code below:
String sql3 = “””
SELECT *
FROM Programmers\n
WHERE Language = ‘Java’;
“””;
The string literal resulting from entering the text block in the jshell tool:
sql3 ==> “SELECT *\nFROM Programmers\n\nWHERE Language = ‘Java’;\n”
Printing the text block sql3 above will give the following result, where the underscore (_) is used as a visual marker to indicate the position after printing the resulting string:
SELECT *
FROM Programmers
WHERE Language = ‘Java’;
_
In the examples so far, the closing delimiter of a text block was specified on the last line by itself, resulting in the last line of text in the text block being terminated by the line terminator that was entered directly. If the last line of text should not be terminated, the closing delimiter can be used at the end of this line as shown below.
String sql4 = “””
SELECT *
FROM Programmers
WHERE Language = ‘Java’;”””; // No line terminator. Closing delimiter ends block.
The string literal resulting from entering the text block in the jshell tool shows no line terminator for the last line of the query:
sql4 ==> “SELECT *\nFROM Programmers\nWHERE Language = ‘Java’;”
The print statement below does not print a newline after the last line:
System.out.print(sql4);
SELECT *
FROM Programmers
WHERE Language = ‘Java’;
Other escape sequences (Table 2.10, p. 38) can also be used in a text block. In contrast to the \n escape sequence, the \Line terminator escape sequence, allowed only in a text block, escapes the line terminator, thus preventing the termination of the current line so that it is joined with the next line.
String sql5 = “””
SELECT * \
FROM Programmers \
WHERE Language = ‘Java’;
“””;
The string literal resulting from entering the text block in the jshell tool shows that the three lines were joined and no characters were substituted for the \Line terminator escape sequence:
sql5 ==> “SELECT * FROM Programmers WHERE Language = ‘Java’;\n”
The result from printing the string sql5 is shown below, where the underscore (_) is used as a visual marker to indicate the position after printing the resulting string.
SELECT * FROM Programmers WHERE Language = ‘Java’;
_
To include a backslash (\) in any context other than line continuation will require escaping the backslash, analogous to using it in a string literal.
By default, any trailing whitespace on a line is removed and line termination normalized. In some cases, it might be necessary to retain trailing whitespace. This can be achieved by using the \s escape sequence. Replacing the last trailing space that should be retained with this escape sequence will preserve any trailing whitespace before the \s escape sequence, including the space replaced by the escape sequence. At (1) below, four trailing spaces are retained.
String sql6 = “””
SELECT *
FROM Programmers \s “””; // (1) No line termination.
// sql6 ==> “SELECT *\nFROM Programmers “
String sql7 = “WHERE Language = ‘Java’;\n”;
System.out.print(sql6 + sql7);
Output from the code, showing the retained spaces, where the underscore (_) is used as a visual marker to indicate the position after printing the resulting string:
SELECT *
FROM Programmers WHERE Language = ‘Java’;
_
Double quotes (“) are treated like any other character and can be used without escaping them in a text block, except when a three double quote sequence is used in the text. It is enough to escape the first double quote in a three double quote sequence, but all three double quotes can also be escaped individually in the sequence. It is also not required that double quotes should be balanced. The code below illustrates using double quotes in a text block.
String fact = “””
The sequence \”””
has “special” meaning in a text block.
“””;
// fact ==> “The sequence \”\”\”\nhas \”special\” meaning in a text block.\n”
System.out.print(fact);
Output from the code, where the underscore (_) is used as a visual marker to indicate the position after printing the resulting string:
The sequence “””
has “special” meaning in a text block.
_
A text block can be used to define the format string for value substitution in formatted text (p. 457). Below, the text block defines the format string used by the formatted() method for substituting values of its arguments.
String query = “””
SELECT *
FROM %s
WHERE %s = ‘%s’;
“””.formatted(“Customers”, “Country”, “Norway”);
System.out.print(query);
Output from the print statement, where the underscore (_) is used as a visual marker to indicate the position after printing the resulting string:
SELECT *
FROM Customers
WHERE Country = ‘Norway’;
_
Leave a Reply