Using Escape Sequences in Text Blocks – Selected API Classes

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.)

Click here to view code image

String sql2 = “””
SELECT *
FROM Programmers\nWHERE Language = ‘Java’;
“””;

The string literal resulting from entering the text block in the jshell tool:

Click here to view code image

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:

Click here to view code image

String sql3 = “””
SELECT *
FROM Programmers\n
WHERE Language = ‘Java’;
“””;

The string literal resulting from entering the text block in the jshell tool:

Click here to view code image

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:

Click here to view code image

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.

Click here to view code image

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:

Click here to view code image

sql4 ==> “SELECT *\nFROM Programmers\nWHERE Language = ‘Java’;”

The print statement below does not print a newline after the last line:

Click here to view code image

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.

Click here to view code image

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:

Click here to view code image

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.

Click here to view code image

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 *

Click here to view code image

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:

Click here to view code image

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.

Click here to view code image

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:

Click here to view code image

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.

Click here to view code image

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:

Click here to view code image

SELECT *
FROM Customers
WHERE Country = ‘Norway’;
_


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *