Incidental Whitespace – Selected API Classes

Incidental Whitespace

So far the lines in the text blocks have all been left-justified. This need not be the case. Indentation can be used if desired. However, one needs to be careful as incidental whitespace from each line is removed by the compiler. Incidental whitespace is indentation that is common to all lines in the text block. It is equal to the number of leading whitespace characters in the least indented lines in the text block. The least indented lines end up with no indentation. The line containing the closing delimiter also plays a part in determining the incidental whitespace to be removed from all lines. Regardless of how much incidental whitespace is removed from the other lines in the text box, all leading whitespace preceding the closing delimiter is always removed, resulting in an empty line.

Whitespace that is not incidental is referred to as essential whitespace.

Figure 8.3 Incidental Whitespace in Text Blocks

In Figure 8.3a, we see that the first and third lines, and the line with the closing delimiter, are the ones that are least indented in the text block. The number of leading whitespace characters in each of these lines is four. The indentation in the second line is six positions. The least number (i.e., four) of leading whitespace characters common to all lines is the incidental whitespace that is removed from each line. Figure 8.3b shows how the contents of the text block will be printed. The underscore (_) is a visual marker that indicates the position after printing the text block, showing that a newline is printed for the last line of the text block. Removing the incidental whitespace preserves the relative indentation of the lines in the text block. Figure 8.3c shows the string literal resulting from entering the text block in the jshell tool.

As stated earlier, the whitespace preceding the closing delimiter is significant in determining incidental whitespace. Table 8.2 shows a few more examples to illustrate how incidental whitespace is calculated and removed in a text block. The last two rows show the text block declaration and the result of printing the text block, respectively. The underscore (_) in each cell in the last row is a visual marker that indicates the position after printing the text block, showing that no newline is printed for the last line of the text blocks in columns (4) and (5). The numbered remarks below refer to the column numbers in Table 8.2. The string literal resulting from entering each text block in the jshell tool is also shown.

(1) The least indentation (0) is determined by the last line with the closing delimiter. No incidental whitespace is removed.

Click here to view code image

tb1 ==> ”    KEEP\n      IT\n        SIMPLE\n”

(2) The least indentation (2) is determined by the last line with the closing delimiter.

Click here to view code image

tb2 ==> ”  KEEP\n    IT\n      SIMPLE\n”

(3) The least indentation (4) is determined by the line containing the word IT. However, all leading whitespace in the last line is removed, not just four spaces as in the other lines—we can see the result in the string literal below:

Click here to view code image

tb3 ==> ”  KEEP\nIT\n  SIMPLE\n”

(4) The least indentation (2) is determined by the last line containing the word SIMPLE and the closing delimiter. Note that the last line is not terminated.

Click here to view code image

tb4 ==> ”  KEEP\n    IT\nSIMPLE”

(5) The least indentation (4) is determined by the lines containing the words KEEP and IT, respectively. Note that in the last line only incidental whitespace is removed, and the line is not terminated.

Click here to view code image

tb5 ==> “KEEP\nIT\n  SIMPLE”

Table 8.2 Incidental Whitespace in Text Blocks

(1)(2)(3)(4)(5)
Least indent is 0Least indent is 2Least indent is 4Least indent is 2Least indent is 4
123456789012345123456789012345123456789012345123456789012345123456789012345
String tb1 =
“””
    KEEP
      IT
        SIMPLE
“””;

String tb2 =
“””
    KEEP
      IT
        SIMPLE
  “””;

String tb3 =
“””
      KEEP
    IT
      SIMPLE
       “””;

String tb4 =
“””
    KEEP
      IT
  SIMPLE”””;

String tb5 =
“””
    KEEP
    IT
      SIMPLE”””;

   
KEEP
      IT
        SIMPLE
_

 
KEEP
    IT
      SIMPLE
_

 
KEEP
IT
  SIMPLE
_

 
KEEP
    IT
SIMPLE
_

KEEP
IT
  SIMPLE
_

A blank line (i.e., one that is empty or contains only whitespace) in a text block is replaced by an empty line—that is, any whitespace it contains is removed.

Incidental whitespace in a text block is processed internally as if by the execution of the String.stripIndent() method. Calling this method on a text block with one or more lines has no effect on the content of the text block, but it will strip any whitespace from a text block that has no line structure.

The reader is encouraged to use the jshell tool to examine the string literal resulting from entering the text blocks presented in this section.

String stripIndent()

Returns a string whose value is this string, with incidental whitespace removed from the beginning and end of every line.

Leave a Reply

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