New feature of JDK 14: text blocks

In a word, text blocks was introduced in jdk13 as the first preview version. Now in jdk14 is the second preview version JEP 368: text blocks.

In our daily work, sometimes we need to use a large section of strings. These strings need line feed, typesetting and escape. This is of course very easy in a text editor. But in Java code, it's a nightmare.

Although the IDE can automatically add line breaks for us, it can even splice strings. But in the eyes of Java programs, many additional codes are added to destroy the beauty of the code. It is unbearable for any programmer with cleanliness.

What should I do? Text blocks is here to save everyone.

for instance

Let's give an intuitive example first, and then analyze the characteristics of text blocks.

Let's take HTML as an example. If we want to print HTML with reduction and format, the traditional method can do this:

String html = "<html>\n" +
              "    <body>\n" +
              "        <p>Hello,world</p>\n" +
              "    </body>\n" +
              "</html>\n";

The above code looks very awkward. Let's see how to do it in text blocks:

String html = """
              <html>
                  <body>
                      <p>Hello,world</p>
                  </body>
              </html>
              """;

Is it refreshing? I want to like the text block immediately.

Don't panic and praise. We have more to discuss.

Indentation orchestration

Someone may have a problem again. The text block is easy to use. Where are the spaces in front of the fields in your output result?

This concept will be introduced here: the English name is indentation, and I translate it into choreography in Chinese.

Let's take a look at the above code. This time, we use the space in front of the code as a dot:

String html = """
..............<html>
..............    <body>
..............        <p>Hello,world</p>
..............    </body>
..............</html>
..............""";

The rule of indentation is to remove the same number of spaces for each line with the lowest "" as the boundary.

The above code output:

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

In the above example, the bottom "" is just at the leftmost position. What happens if you move "" four spaces to the right?

String html = """
..............<html>
..............    <body>
..............        <p>Hello,world</p>
..............    </body>
..............</html>
..................""";

Output results:

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

We see that the output result is unchanged, so we get another conclusion: if the "" moves to the right, the leftmost row in the text block shall prevail.

If we move "" four bits to the left, we will find that the final output result has four spaces in front of each line.

This function is related to the new string:: stripindent() added to string.

Escape escape

Let's look at an intuitive example:

    @Test
    public void useEscape(){
        String code =
                """
                "
                ""
                \s\s\s\s\s保留这行前面的空白
                String text = \"""
                    这里展示的是escape的用法!
                \""";
                跟大家说个密码,这一行很长,我准备分行\
                来写,哈哈!
                """;
        log.info("{}",code);
    }

Output results:

”
""
     保留这行前面的空白
String text = """
    这里展示的是escape的用法!
""";
跟大家说个密码,这一行很长,我准备分行来写,哈哈!

First of all, you can see that one double quotation mark and two double quotation marks are written directly without escape. Three double quotes need to be escaped.

In addition, \ s represents a space. It can be used when needed.

Inserting \ directlyat the end of a line indicates that the line is too long to end.

This escape function is also used for the new method translateescapes () of string.

Formatted

Finally, let's introduce the formatting of text block, which is the same as that of string. For example, SQL:

    @Test
    public void useMethod(){
        String query1 = """
                SELECT `EMP_ID`,`LAST_NAME` FROM `EMPLOYEE_TB`
                WHERE `CITY` = '%s'
                ORDER BY `EMP_ID`,`LAST_NAME`;
                """;

        log.info(query1.formatted("我是一个参数"));
    }

Output results:

SELECT `EMP_ID`,`LAST_NAME` FROM `EMPLOYEE_TB`
WHERE `CITY` = '我是一个参数'
ORDER BY `EMP_ID`,`LAST_NAME`;

In the above example, we use% s to define placeholders.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>