Arv*_*ash -4 java java-text-blocks java-15
Java SE 13 引入了文本块(或多行字符串)功能。它与现有的字符串表示有什么区别和相似之处?
Arv*_*ash 36
一个文本块是一个多行字符串字面量和功能提供了一个清晰的方式来格式化可预测的方式串,不使用最转义序列。它以"""(三个双引号)开头和结尾,例如
public class Main {
public static void main(String[] args) {
String text = """
<html>
<head>
<title>Hello World</title>
</head>
<body>
Java rocks!
</body>
</html>""";
System.out.println(text);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
<html>
<head>
<title>Hello World</title>
</head>
<body>
Java rocks!
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
使用传统的字符串表示,代码看起来像
public class Main {
public static void main(String[] args) {
String text = "<html>\n"
+ " <head>\n"
+ " <title>Hello World</title>\n"
+ " </head>\n"
+ " <body>\n"
+ " Java rocks!\n"
+ " </body>\n"
+ "</html>";
System.out.println(text);
}
}
Run Code Online (Sandbox Code Playgroud)
另一个主要区别是文本块以三个双引号字符开头,后跟一个行终止符,这与传统的字符串表示不同。它的意思是
文本块不能放在一行上。
文本块的内容不能跟在同一行的三个开始双引号后面。
String str = "Hello World!"; // The traditional string
String textBlock = """
Hello World!
"""; // The text block
String notAllowed = """Hello World!"""; // Error
// The following code will fail compilation
String notAllowed2 ="""Hello
World!
""";
Run Code Online (Sandbox Code Playgroud)
关于缩进的说明:
编译器将整个文本块向左移动,然后保留指定的间距。
String str1 = """
Hello""";
^^^<-----These three whitespace characters will be retained
Run Code Online (Sandbox Code Playgroud)
演示:
public class Main {
public static void main(String[] args) {
// Text block without a line break at the end
String str1 = """
Hello""";
// Text block with a line break at the end
String str2 = """
Hello
""";
// Text block with three whitespace in the beginning and a line break at the end
String str3 = """
World!
""";
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
System.out.println("Java rocks!");
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
Hello
Hello
World!
Java rocks!
Run Code Online (Sandbox Code Playgroud)
它在 Java SE 13 和 Java SE 14 中作为预览功能仍然可用,并已在 Java SE 15 中标准化。对于 Java SE 13 和 14,与任何预览功能一样,必须使用--enable-preview选项编译和执行,例如
编译:
javac --enable-preview --release 13 Main.java
Run Code Online (Sandbox Code Playgroud)
执行:
java --enable-preview Main
Run Code Online (Sandbox Code Playgroud)
对,他们是。的文本块被编译为相同类型,传统的String值即字节代码不传统区分String值和文本块。
public class Main {
public static void main(String[] args) {
String str1 = "Hello World!";
String str2 = """
Hello World!""";
System.out.println(str1 == str2);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
true
Run Code Online (Sandbox Code Playgroud)
是的,文本块可以以相同的方式连接到传统字符串值或另一个文本块,传统String值是连接的。如上所述,字节码不区分传统String值和文本块。
public class Main {
public static void main(String[] args) {
String str1 = "Hello ";
String str2 = """
World!""";
String str3 = """
Java rocks!
""";
System.out.println(str1 + str2);
System.out.println(str1 + (str2 + str3));
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
Hello World!
Hello World! Java rocks!
Run Code Online (Sandbox Code Playgroud)
请注意,我已经把空格之后Hello在str1之前的另一个空白Java rocks!的str3。
是的,转义序列将以传统方式进行评估,例如
public class Main {
public static void main(String[] args) {
String text = """
<html>
<head>
<title>Hello World</title>
</head>
<body>
Java\n\t\trocks!
</body>
</html>""";
System.out.println(text);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
<html>
<head>
<title>Hello World</title>
</head>
<body>
Java
rocks!
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
是的,您可以使用%s或替换文本块中的参数,$<<replaceable-parameter>>如下所示:
public class Main {
public static void main(String[] args) {
String text = """
What is the distance between %s and %s?""";
System.out.println(String.format(text, "earth", "moon"));
System.out.println(String.format(text, "earth", "mercury"));
// Alternative-1
text = """
What is the distance between $celestial1 and $celestial2?""";
System.out.println(text.replace("$celestial1", "earth").replace("$celestial2", "moon"));
// Alternative-2 using the deprecated String#formatted
text = """
What is the distance between %s and %s?""";
System.out.println(text.formatted("earth", "moon"));
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
What is the distance between earth and moon?
What is the distance between earth and mercury?
What is the distance between earth and moon?
What is the distance between earth and moon?
Run Code Online (Sandbox Code Playgroud)