单行文本块
Text blocks in a single line
是否可以使用Javas 文本块功能(Java15)但只写单行?
看来我是被迫写多行的
例如,我想把它写在一行中,以避免转义其中的"
String text = """<a href="https://sparkjava.com">https://sparkjava.com/</a>""";
但是编译不了,我不得不写
String text = """
<a href="https://sparkjava.com">https://sparkjava.com/</a>""";
相反。
我是不是忽略了什么?
不可以,因为文本块 require a newline as part of the opening delimiter:
The opening delimiter is a sequence that starts with three double quote characters ("""), continues with zero or more space, tab, and form feed characters, and concludes with a line terminator.
所以你不能有只有一行的文本块。
否 - 这是不可能的,因为在 """
运算符
之后有 mandatory 新行
A text block begins with three double-quote characters followed by a line terminator. You can't put a text block on a single line, nor can the contents of the text block follow the three opening double-quotes without an intervening line terminator. The reason for this is that text blocks are primarily designed to support multi-line strings, and requiring the initial line terminator simplifies the indentation handling rules (see the section below, Incidental White Space).
是否可以使用Javas 文本块功能(Java15)但只写单行?
看来我是被迫写多行的
例如,我想把它写在一行中,以避免转义其中的"
String text = """<a href="https://sparkjava.com">https://sparkjava.com/</a>""";
但是编译不了,我不得不写
String text = """
<a href="https://sparkjava.com">https://sparkjava.com/</a>""";
相反。
我是不是忽略了什么?
不可以,因为文本块 require a newline as part of the opening delimiter:
The opening delimiter is a sequence that starts with three double quote characters ("""), continues with zero or more space, tab, and form feed characters, and concludes with a line terminator.
所以你不能有只有一行的文本块。
否 - 这是不可能的,因为在 """
运算符
A text block begins with three double-quote characters followed by a line terminator. You can't put a text block on a single line, nor can the contents of the text block follow the three opening double-quotes without an intervening line terminator. The reason for this is that text blocks are primarily designed to support multi-line strings, and requiring the initial line terminator simplifies the indentation handling rules (see the section below, Incidental White Space).