Java13 - 文本块
创建于 2024-12-02 /
19
字体:
[默认]
[大]
[更大]
Java13 引入了文本块来处理多行字符串,如 JSON/XML/HTML 等。这是一个预览功能。
文本块允许在不使用 \r\n 的情况下轻松编写多行字符串。
文本块字符串具有与字符串相同的方法,如 contains()、indexOf() 和 length() 函数。
示例
考虑以下示例 −
ApiTester.java
public class APITester { public static void main(String[] args) { String stringJSON = "{\r\n" + ""Name" : "Mahesh",\r\n" + ""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 13 APITester.java $java --enable-preview APITester
输出
{ "Name" : "Mahesh", "RollNO" : "32" } { "name" : "Mahesh", "RollNO" : "32" } Contains: true indexOf: 15 Length: 45
0 人点赞过