In this tutorial, we will see how to formatting String in Java 13, in Java preparing a snippet of HTML, XML and SQL in a string literal is a big challenge, it requires significant effort with escapes and concatenations with escape sequences. However, these snippets are challenging to read and maintain it.

Java 13 Text Blocks – Formatting String in Java:

Java 13 simplifies this task by introducing Text Blocks. A text block is a multi-line string literal that avoids the usage of escape sequences, and it automatically formats the string in the given way. Typically it enhances the readability of strings in Java language.

Formatting String in Java 13:

A text block is a new kind of literal in Java Language, and it consists of zero or more characters, enclosed by open and closing delimiters. The opening and closing delimiters should be three double quotes (“””). The content may or may not contain double quote (“), single quote (‘), backslash (\) and any direct, special character.

Legacy way of using the above characters in a string:

// HTML string containing double quote and line break.
String html="<html>\n" +
                "       <body>\n" +
                "           <p>\"Hello, world\"</p>\n" +
                "       </body>\n" +
                "  </html>";
// Text string with double quote
String str2 = "Highlight \"ME\" ";
// Windows folders path
String path = "D:\softwares\jdk-13.0.1\bin";

Java 13 Text Blocks Example:

As stated previously, text blocks are multi-line string literals these should be enclosed with three double quotes (“””).

HTML Text Blocks:

String html = """
                <html>
                  <body>
                      <p>"Hello, world"</p>
                  </body>
                </html>
                """;
System.out.println(html);

Output:

<html>
  <body>
      <p>"Hello, world"</p>
  </body>
</html>

SQL Text Blocks:

String sql = """
            select `employee_name`,`salary`, from employee where employee order by employee_id;
        """;
System.out.println(sql);

Output:

select `employee_name`,`salary`, from employee where employee order by employee_id;

Path Text Blocks:

String win_path= """
        D:\softwares\jdk-13.0.1\path
""";

Output:

D:\softwares\jdk-13.0.1\path

Text Blocks with Escape Sequences:

A text block can contain escape sequences such as \n, \t, \’, \”, as if like normal strings.

// Escape sequences..
String html_seq = """
      <html>\n
          <body>\n
              <p>Hello, world</p>\n
          </body>\n
      </html>\n
      """;
System.out.println(html_seq);

It gives an extra line after each line of text block.

Output:

<html>

    <body>

        <p>Hello, world</p>

    </body>

</html>

Text Block inside another Text Block:

A text block can have one or more text blocks inside it; it can be achieved using escape sequences.

    String outerblock =
            """
String inner = \"""
    A text block inside a text block
\""";
""";
    System.out.println(outerblock);

Output:

String inner = """
    A text block inside a text block
""";

Concatenating Text Blocks:

Text blocks can be concatenated with another text block or with a standard string.

// Test Block with String concatenation
String data = "Hello"+
                """
 World :)
""";
        System.out.println(data);

Concatenating two text blocks with dynamic values.

        String name = " user";
        String data = """
        Hello"""+ name+
                 """
 ! :)
""";
System.out.println(data);

Output:

Hello user ! :)

References:

JEP 355- Text Blocks

JEP 12 – Preview Language and VM Features

Happy Learning 🙂