- Java 14 Tutorial
- Java 14 - Home
- Java 14 - Overview
- Java 14 - Environment Setup
- Java 14 Language Changes
- Java 14 - Switch Expressions
- Java 14 - Text Blocks
- Java 14 - pattern for instanceOf
- Java 14 - NullPointerException
- Java 14 JVM Changes
- Java 14 - Packaging Tools
- Java 14 - NUMA Aware G1
- Java 14 - Others
- Java 14 - Deprecation & Removals
- Java 14 Useful Resources
- Java 14 - Quick Guide
- Java 14 - Useful Resources
- Java 14 - Discussion
Java 14 - 文本块
Java 13 引入了文本块来处理多行字符串,如 JSON/XML/HTML 等,这是预览功能。在 Java 14 中,我们提供了文本块的第二次预览。现在文本块有以下增强功能 -
\ − 表示行结束,以防要开始新行。
\s - 表示单个空格。
例子
考虑以下示例 -
ApiTester.java
public class APITester { public static void main(String[] args) { String stringJSON = "{\r\n" + "\"Name\" : \"Mahesh\"," + "\"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()); } }
编译并运行程序
$javac -Xlint:preview --enable-preview -source 14 APITester.java $java --enable-preview APITester
输出
{ "Name" : "Mahesh","RollNO" : "32" } { "name" : "Mahesh", "RollNO" : "32" } Contains: true indexOf: 15 Length: 45