非法文本块打开定界符序列,缺少行终止符 [英] illegal text block open delimiter sequence, missing line terminator
问题描述
Java 13即将到来,所以我开始研究它的新功能,其中之一是文本块.
Java 13 is coming, so I started studying its new features, one of which is text blocks.
我写了一个简单的程序
public final class Example {
public static void main(String[] args) {
final String greeting = """Hello
It's me, Andrew!""";
System.out.println(greeting);
}
}
我希望看到
Hello
It's me, Andrew!
我得到的是编译错误提示
What I got is a compilation error saying
非法文本块打开定界符序列,缺少行终止符
illegal text block open delimiter sequence, missing line terminator
推荐答案
文本块的上下文必须从新行开始.
The context of your text block must start from a new line.
public final class Example {
public static void main(String[] args) {
final String greeting = """
Hello
It's me, Andrew!""";
System.out.println(greeting);
}
}
打印
Hello
It's me, Andrew!
摘录自 JEP 355:文本块(预览):
一个文本块由零个或多个内容字符组成,并用打开和关闭定界符将其括起来.
A text block consists of zero or more content characters, enclosed by opening and closing delimiters.
分隔符是由三个双引号字符("""
)组成的序列,后跟零个或多个空格,后跟一个行终止符.内容从开头定界符的行终止符之后的第一个字符开始.
The opening delimiter is a sequence of three double quote characters ("""
) followed by zero or more white spaces followed by a line terminator. The content begins at the first character after the line terminator of the opening delimiter.
不过,您不必在内容的末尾放置一个行终止符.
You don't necessarily have to put a line terminator at the end of your content, though.
结束分隔符是三个双引号字符的序列. 内容在结束定界符的第一个双引号之前的最后一个字符处结束.
final String greeting = """
Hello
It's me, Andrew!
""";
会意味着
Hello
It's me, Andrew!
<an empty line here>
我发现还不清楚,所以我不得不与社区分享.
I find it extremely unclear, so I had to share this with the community.
这篇关于非法文本块打开定界符序列,缺少行终止符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!