All the escape sequences that can be used with the String, can be used in text blocks as well.
But some escape sequences are not required to be used within text blocks. The actual character can be used directly instead. The most common escape sequence that can and should be avoided where possible in text blocks is the new line character '\n'.
At the end of each line of the multi-line string literal represented by the text block, new line character '\n' is included by Java compiler when processing the text block.
We saw many examples of this in the previous posts.
There are a few tricks that we need to be aware of. First, lets see what happens if we explicitly include '\n' at the end of each line inside the text block
Below code shows a regular text block and the same with '\n' included at the end of each line
public class Main {
public static void main(String[] args) {
String poemTextBlock = """
The woods are lovely, dark and deep,
But I have promises to keep,
And miles to go before I sleep,
And miles to go before I sleep.
""";
String poemTextBlockWithNewLine = """
The woods are lovely, dark and deep,\n
But I have promises to keep,\n
And miles to go before I sleep,\n
And miles to go before I sleep.
""";
System.out.println("Text block: \n"+poemTextBlock);
System.out.println("Text block with new line: \n"+poemTextBlockWithNewLine);
}
}
The '\n' at the end of each line introduces an additional new line between each of the lines and produces the output shown below
Here we have two '\n' on the first line of text block, with a tab included in between.
How does this get processed? There are four leading tab spaces in each of the lines of the text block, but between the two new line characters there is just one tab space. Does this impact the indentation of the lines making the text block. More specifically, will this modify the position of left margin for incidental whitespace stripping?
The above text block when printed has a value shown below
As you can see, the left margin is not affected by the whitespaces included between '\n' characters. Also note the presence of tab characters at the beginning of the 2nd line, indicating it is preserved and has not got stripped away in the processing.
This is because the escape translation happens as a last step in the compilation process.
The left margin gets identified and incidental whitespaces gets stripped away before '\n' escape sequence gets processed in our example. Escape sequence processing happens as a last step, making the '\n' characters include additional line feeds within the string.
This is also the reason why we may have to use '\n' explicitly - to include empty line with specific no. of whitespace characters without impacting the margin of other lines within a text block.
There is no other way of doing this when defining a text block.
Sample code used in this post can be downloaded from https://github.com/ashokkumarta/awesomely-java/tree/main/2021/03/Language-Features/Text-blocks/Escape-sequences-in-text-blocks-1
No comments:
Post a Comment