Indenting Lines in a String – Selected API Classes

Indenting Lines in a String

The indent() method allows indentation of lines in a string to be adjusted.

String indent(int n)

Adjusts the indentation of each line of this string based on the value of n, and normalizes line termination characters.

This string is conceptually separated into lines using the String.lines() method (p. 445). Each line is then adjusted depending on the value of n, and then terminated with a line feed (“\n”). The resulting lines are then concatenated and the final string returned.

If n > 0 then n spaces are inserted at the beginning of each line. This also applies to an empty line.

If n == 0 then the line remains unchanged. However, line terminators are still normalized.

If n < 0 then at most n whitespace characters are removed from the beginning of each line, depending on the number of leading whitespace characters in the line. Each whitespace character is treated as a single character; specifically, the tab character “\t” is considered a single character and is not expanded.

The example below illustrates how indentation is adjusted depending on the value passed to the indent() method. The string cmds constructed at (0) has four lines, where the third line is an empty line. The first, second, and last lines are indented by one, two, and three spaces, respectively. The first line is terminated with the return character (\r), and the last line is not terminated at all. The other lines are terminated normally with a newline character (\n). Keep also in mind that the string cmds is immutable.

Click here to view code image

String cmds = ” Attention!\r  Quick march!\n\n   Eyes left!”; // (0)
String str1 = cmds.indent(0);                                 // (1)
String str2 = cmds.indent(3);                                 // (2)
String str3 = cmds.indent(-1);                                // (3)
String str4 = cmds.indent(-2);                                // (4)
String str5 = cmds.indent(-3);                                // (5)

The lines numbered (1) to (5) below show the resulting string from the method calls above from (1) to (5), respectively, applied to the string cmds at (0) above. The jshell tool can readily be used to execute the method calls above to examine the resulting strings. Note that termination of all lines is normalized with the newline character (\n).

Click here to view code image

(0) cmds ==> ” Attention!\r  Quick march!\n\n   Eyes left!”
(1) str1 ==> ” Attention!\n  Quick march!\n\n   Eyes left!\n”
(2) str2 ==> ”    Attention!\n     Quick march!\n   \n      Eyes left!\n”
(3) str3 ==> “Attention!\n Quick march!\n\n  Eyes left!\n”
(4) str4 ==> “Attention!\nQuick march!\n\n Eyes left!\n”
(5) str5 ==> “Attention!\nQuick march!\n\nEyes left!\n”

In Table 8.1, the columns correspond to the resulting strings from (1) to (5) above. The last row in Table 8.1 shows the output when the resulting strings are printed.

The underscore (_) in each cell in the last row is a visual marker that indicates the position after printing the resulting string. Note that the empty line at (0) above is indented (marked visually as sss), as shown in column (2). A value less than -3 does not change the result shown in column (5).

Table 8.1 Indenting Lines in a String

(1)(2)(3)(4)(5)
n = 0n = 3n = -1n = -2n = -3
123456789123456789123456789123456789123456789
Attention!
 Quick march!

  Eyes left!
_

Attention!
    Quick march!
sss

    Eyes left!
_

Attention!
Quick march!

 Eyes left!
_

Attention!
Quick march!

Eyes left!
_

Attention!
Quick march!

Eyes left!
_


Comments

Leave a Reply

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