Hello everyone, I'm DD, today I will continue to learn some new features of Java!
Suppose there is such a scenario, we need to make a tool. It is used to automatically generate project documents, and the documents can be viewed through the browser, so the final output must be a bunch of html files. In order to make these html files easier to read, good formatting must be maintained, the line break of the line, the indent of the indent.
Then, when organizing the final content, you might write something like this:
String html =
"<html>\n" +
"<body>\n"+
" <h1>Java 15 新特性:文本块 | 程序猿DD</h1>\n"+
" <p>didispace.com</p>\n"+
"</body>\n"+
"</html>\n";
Of course, maybe you can improve it and useStringBuilder
orStringBuffer
to optimize, but no matter what you use to write, you can't escape some content that needs to be escaped, such as when splicing the content above newline\n
.
Once there is a large amount of content to be escaped, it increases the complexity of our writing content. The higher the complexity, the more likely we are to make mistakes.
Therefore, a new feature has been added in Java 15: Text Blocks to help us more conveniently process multi-line string literals.
For the above string content, in Java 15, we just need to write:
String html = """
<html>
<body>
<h1>Java 15 新特性:文本块 | 程序猿DD</h1>
<p>didispace.com</p>
</body>
</html>
""";
Isn't it much simpler? If you haven't used it before, give it a try! Click to view the accompanying video. If you encounter difficulties in the learning process? You can join our high-quality technical exchange group , participate in exchanges and discussions, and learn and progress better! Also, don't walk away, follow me! Continue to update the new Java feature tutorial , written in document mode, and the reading experience is better!
Welcome to my public account: Programmer DD. Learn about cutting-edge industry news for the first time, share in-depth technical dry goods, and obtain high-quality learning resources
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。