Text Blocks in Java


If you want to write multi line string in java , you must \n escape sequence which inserts new line. 
For example
public class Test {

public static void main(String[] args) {
String addressWithOutTextBlock = "H.No. 1234\n" + "Hitec city\n" + "Madhapur\n" + "Hyderabad - 500081";
System.out.println("Address WithOut TextBlock");
System.out.println(addressWithOutTextBlock);

}
}
Output:
Address WithOut TextBlock
H.No. 1234
Hitec city
Madhapur
Hyderabad - 500081

  • Using text block feature , you don't have to use \n to create new line.
  • It was added in java 15 version. To use this feature, you must use jdk 15 or higher version.
  • You write a text block using three double quotes characters. 
  • You can write any number of lines text within pair of  three double quotes characters.

public class Test {

public static void main(String[] args) {
String addressWithTextBlock = """
H.No. 1234
Hitec city
Madhapur
Hyderabad - 500081
""";
System.out.println("Address With TextBlock");
System.out.println(addressWithTextBlock);
}

}
Output:
Address With TextBlock
H.No. 1234
Hitec city
Madhapur
Hyderabad - 500081

  • Import point note, you must start writing text in next line of starting three double quote characters. 
  • If you write in the same line of starting three double quote characters, you get compile time error.
  • The ending three double quote characters, adds a new line after our text. 
  • If you don't want new line, you can write ending three double quote characters at the end of our text like below.
String addressWithTextBlock = """
H.No. 1234
Hitec city
Madhapur
Hyderabad - 500081""";
  • You can also use double quotes within text block. You don't have to use \" escape sequence.
String addressWithTextBlock = """
H.No. 1234
"Hitec city"
Madhapur
Hyderabad - 500081""";
Output:
H.No. 1234
"Hitec city"
Madhapur
Hyderabad - 500081
  • You can use two new escape sequences in text blocks \s, \. 
  • \s adds space , \ indicates line continuation. 
String addressWithTextBlock = """
H.No.\s1234
Hitec city \
Madhapur
Hyderabad - 500081""";
Output:
H.No. 1234
Hitec city Madhapur
Hyderabad - 500081
  • If there are any white spaces before each line, they will be removed as per below rules. 
  • If every line has same number of white spaces , those all white spaces are removed from each line.
String addressWithTextBlock = """
 H.No.1234
 Hitec city 
 Madhapur
 Hyderabad - 500081
""";
Output:
H.No.1234
Hitec city
Madhapur
Hyderabad - 500081
  • If there are odd number of white spaces before each line , least of number of white spaces will be removed. 
String addressWithTextBlock = """
   H.No.1234
     Hitec city 
       Madhapur
  Hyderabad - 500081
""";
Output:
 H.No.1234
   Hitec city
     Madhapur
Hyderabad - 500081
  • The ending three double quotes also decides how many white spaces to be removed. 
  • If the ending three double quotes placed before text, white spaces till that """ characters will be removed.
  • If the ending three double quotes placed after text, white spaces till first character of text will be removed.
String addressWithTextBlock = """
 H.No.1234
 Hitec city 
 Madhapur
 Hyderabad - 500081
""";
Output:
H.No.1234
Hitec city
Madhapur
Hyderabad - 500081
String addressWithTextBlock = """
      H.No.1234
      Hitec city 
      Madhapur
      Hyderabad - 500081
  """;
Output:
    H.No.1234
    Hitec city
    Madhapur
    Hyderabad - 500081

I hope you have learned something new .  Thanks for reading. If you have questions, please leave a comment below. You can watch same content video in my youtube channel Full Stack Adda .  
Happy Coding 😀. Jai Hind.



No comments:

Post a Comment

Different ways to run Spring boot App

 What are the different ways to run Spring boot app ?