In this post under Java Core, I will show you with example how indentation is calculated in Text Block.
Below is the main class with three variety of text block.
Example3
package core.string;public class Example3 { public static void main(String[] args) { String textBlock1 = """ <html> <head> <title>TextBlock1<title> </head> <body>Body of TextBlock1</body> </html> """; String textBlock2 = """ <html> <head> <title>TextBlock2<title> </head> <body>Body of TextBlock2</body> </html> """; String textBlock3 = """ <html> <head> <title>TextBlock3<title> </head> <body>Body of TextBlock3</body> </html> """; String textBlock4 = """ <html> <head> <title>TextBlock4<title> </head> <body>Body of TextBlock4</body> </html> """; String textBlock5 = """ <html> <head> <title>TextBlock5<title> </head> <body>Body of TextBlock5</body> </html> """; System.out.println("-------------------------------------"); System.out.println(textBlock1); System.out.println("-------------------------------------"); System.out.println(textBlock2); System.out.println("-------------------------------------"); System.out.println(textBlock3); System.out.println("-------------------------------------"); System.out.println(textBlock4); System.out.println("-------------------------------------"); System.out.println(textBlock5); System.out.println("-------------------------------------"); }}
In the above code, for “textBlock1”, the compiler checks the whitespaces from line 6 to 11. It shortlists the lines in which text appears first compared to other lines. So in this case it is line 6 and 11. Since text in both line start at same column, one of the two is chosen, in our case lets choose line 6. The text in line 6 is compared with text in line 12 containing ending 3 double quotes. Since text at line 6 starts at same column as the ending 3 double quotes at line 12. No indentation is added to text.
For “textBlock2”, the compiler checks the whitespaces from 14 to 19. It shortlists the lines in which text appears first compared to other lines. So in this case it is line 14 and 19. Since text in both lines start at same column, one of the two is chosen, in our case lets choose line 14. The text in line 14 is compared with text in line 20 containing ending 3 double quotes. The text at line 14 starts at column say c1, whereas ending 3 double quotes at line 20 starts at column c2. Since c2 < c1, The whitespaces left of column c2 in all the lines (i.e., from 14 to 19) are stripped away. Whatever whitespaces are left after c2 becomes part of the text as indentation. So c2 – c1 whitespaces are added as indentation to the text.
For “textBlock3”, the compiler checks the whitespaces from line 22 to 27. It shortlists the lines in which text appears first compared to other lines. So in this case it is line 22 and line 27. Since text in both lines start at the same column, one of the two is chosen, in our case lets choose line 22. The text in line 22 is compared with text in line 28 containing ending 3 double quotes. The text at line 22 starts at column c1, whereas ending 3 double quotes at line 28 starts at column c2. Since c1 < c2, the whitespaces left of column c1 in all the lines (i.e., from 22 to 27) are stripped away. Whatever whitespaces are left after column c1 becomes part of the actual text as indentation. In this case since there is no whitespaces left so there will be no indentation.
For “textBlock4”, the compiler checks the whitespaces from line 31 to 36. It shortlists the lines in which text appears first compared to other lines. So in this case it is line 36 only. The text in line 36 is compared with text in line 37 containing ending 3 double quotes. The text at line 36 starts at column c1, and ending 3 double quotes at line 37 also starts at same column c1. The whitespaces left of column c1 in all the lines (i.e., from 31 to 36) are stripped away. Whatever whitespaces are left after column c1 becomes part of the actual text as indentation. In this case since there is no whitespaces left so there will be no indentation.
For “textBlock5”, the compiler checks the whitespaces from line 40 to 45. It shortlists the lines in which text appears first compared to other lines. So in this case it is line 40 only. The text in line 40 is compared with text in line 46 containing ending 3 double quotes. The text at line 40 starts at column c1, and ending 3 double quotes at line 46 also starts at column c1. The whitespaces left of column c1 in all the lines (i.e., from 40 to 45) are stripped away. Whatever whitespaces are left after column c1 becomes part of the actual text as indentation. In this case since there is no whitespaces left so there will be no indentation.
In this way indentation is calculated.
Below is the output for your reference.
Output
-------------------------------------<html> <head> <title>TextBlock1<title> </head> <body>Body of TextBlock1</body></html>------------------------------------- <html> <head> <title>TextBlock2<title> </head> <body>Body of TextBlock2</body> </html>-------------------------------------<html> <head> <title>TextBlock3<title> </head> <body>Body of TextBlock3</body></html>------------------------------------- <html> <head> <title>TextBlock4<title> </head> <body>Body of TextBlock4</body></html>-------------------------------------<html> <head> <title>TextBlock5<title> </head> <body>Body of TextBlock5</body> </html>-------------------------------------