logo

Text Blocks


Show

Java 13 introduces textual content blocks to deal with multiline strings like JSON/XML/HTML etc. It is a preview feature.

  • Text Block permits to write down multiline strings without problems without the usage of \r\n.
  • Text Block strings have identical techniques as string-like contains(), indexOf() and length() functions.

Examine the Below-Given Example:

ApiTester.java

public class APITester {

   public static void main(String[] args) {
      String stringJSON = "{\r\n" 
         + "\"Name\" : \"Mahesh\",\r\n" 
         + "\"RollNO\" : \"32\"\r\n" 
         + "}";  

      System.out.println(stringJSON);

  String textBlockJSON = """
         {
            "name" : "Mahesh",
            "RollNO" : "32"
         }
         """;
      System.out.println(textBlockJSON);
  System.out.println("Contains: " + textBlockJSON.contains("Mahesh"));
  System.out.println("indexOf: " + textBlockJSON.indexOf("Mahesh"));
  System.out.println("Length: " + textBlockJSON.length());
   }   
}

Assemble and Run the Program

$javac -Xlint:preview --enable-preview -source 13 APITester.java

$java --enable-preview APITester

Output

{
"Name" : "Mahesh",
"RollNO" : "32"
}
{
   "name" : "Mahesh",
   "RollNO" : "32"
}

Contains: true
indexOf: 15
Length: 45