30% Therapy – 40% Practice – 30% Work project

Java – Text Blocks



Java made text blocks in Java 15 as a standard feature to handle multiline like // etc. It was introduced in Java 13 as a preview feature.

  • Text Block allows to write multiline strings easily without using rn.

  • Text Block string have same methods as string () like , , and functions.

Purpose of introducing text block is mainly to declare multi-line strings most efficiently. Prior to text block, we can declare multi-line strings using string concatenation, string builder append method, string join method but that approach is quite messy. As we have to use line terminators, delimiters etc to mark a new line. Text block provides a better and alternate approach to define multiline string using a “””, 3 double-quotes mark.

Text Block Syntax

A text block is an enhancement to existing String object with special syntax where string content should starts with “”” with newline and ends with “””. Any content within “”” will be used as-is.

String textBlockJSON = """
   {
      "name" : "Mahesh",
      "RollNO" : "32"
   }
   """;

Equivalent String can be written using older syntax as shown below:

String stringJSON = "{rn" 
         + "   "Name" : "Mahesh",rn" 
         + "   "RollNO" : "32"rn" 
         + "}";  

Example of Java Text Block

In this example, we”ve printed the json string using text block as well as using string concatenation.

package com.tutorialspoint;

public class Tester {

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

      System.out.println(stringJSON);

      String textBlockJSON = """
      {
         "name" : "Mahesh",
         "RollNO" : "32"
      }
      """;
      System.out.println(textBlockJSON);
   }   
}

Output

Let us compile and run the above program, this will produce the following result −

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

Text Block String Operations

Text block is same as String and can be compared using or equal operator.

// compare the content, 
textBlockJSON.equals(stringJSON);
	      
// compare the objects
textBlockJSON == stringJSON;

Text block supports all string operations like indexOf(), contains() etc.

// check if text block contains a provided string or not
textBlockJSON.contains("Mahesh");

// get the length of string content
textBlockJSON.length()

Example: Text Block String Operations in Java

In this example, we”ve performed various string operations and compared text block with an equivalent string.

package com.tutorialspoint;

public class Tester {

   public static void main(String[] args) {
      String stringJSON = "Mahesh";

      String textBlockJSON = """
      Mahesh""";
      // compare the content
      System.out.println(textBlockJSON.equals(stringJSON));

      // compare the objects
      System.out.println(textBlockJSON == stringJSON);

      // text block supports all string operations
      System.out.println("Contains: " + textBlockJSON.contains("Mahesh"));
      System.out.println("indexOf: " + textBlockJSON.indexOf("Mahesh"));
      System.out.println("Length: " + textBlockJSON.length());
   }   
}

Output

Let us compile and run the above program, this will produce the following result −

true
true
Contains: true
indexOf: 0
Length: 6

Text Block Methods

  • stripIndent() – removes incidental white spaces from the start and end of the string.

  • translateEscapes() – translate the escape sequences as per the string syntax.

  • formatted() – similar to String format() method to support formatting in text block strings.

Example

Consider the following example −

ApiTester.java

public class APITester {

   public static void main(String[] args) {
	  String textBlockJSON = """
         {
            "name" : "%s",
            "RollNO" : "%s"
         }
         """.formatted("Mahesh", "32");
      System.out.println(textBlockJSON);
   }   
}

Output

{
   "name" : "Mahesh",
   "RollNO" : "32"
}
Translate »