string Groovy 多行字符串有什么问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5079797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:59:50  来源:igfitidea点击:

What's wrong with Groovy multi-line String?

stringgroovymultiline

提问by yegor256

Groovy scripts raises an error:

Groovy 脚本引发错误:

def a = "test"
  + "test"
  + "test"

Error:

错误:

No signature of method: java.lang.String.positive() is 
applicable for argument types: () values: []

While this script works fine:

虽然这个脚本工作正常:

def a = new String(
  "test"
  + "test"
  + "test"
)

Why?

为什么?

回答by tim_yates

As groovy doesn't have EOL marker (such as ;) it gets confused if you put the operator on the following line

由于 groovy 没有 EOL 标记(例如;),如果您将运算符放在以下行上,它会感到困惑

This would work instead:

这将起作用:

def a = "test" +
  "test" +
  "test"

as the Groovy parser knows to expect something on the following line

因为 Groovy 解析器知道在以下行中期待一些东西

Groovy sees your original defas three separate statements. The first assigns testto a, the second two try to make "test"positive (and this is where it fails)

Groovy 将您的原始def声明视为三个独立的语句。第一个赋值testa,第二个赋值为"test"正(这就是它失败的地方)

With the new Stringconstructor method, the Groovy parser is still in the constructor (as the brace hasn't yet closed), so it can logically join the three lines together into a single statement

使用new String构造函数方法,Groovy 解析器仍在构造函数中(因为大括号尚未关闭),因此它可以在逻辑上将三行连接到一个语句中

For true multi-line Strings, you can also use the triple quote:

对于真正的多行字符串,您还可以使用三重引号:

def a = """test
test
test"""

Will create a String with test on three lines

将在三行上创建一个带有测试的字符串

Also, you can make it neater by:

此外,您可以通过以下方式使其更整洁:

def a = """test
          |test
          |test""".stripMargin()

the stripMarginmethodwill trim the left (up to and including the |char) from each line

stripMargin方法将修剪|每一行的左侧(直到并包括字符)

回答by sschuberth

Similar to stripMargin(), you could also use stripIndent()like

与 类似stripMargin(),您也可以使用stripIndent() 之类的

def a = """\
        test
        test
        test""".stripIndent()

Because of

因为

The line with the least number of leading spaces determines the number to remove.

前导空格数最少的行决定了要删除的数量。

you need to also indent the first "test" and not put it directly after the inital """(the \ensures the multi-line string does not start with a newline).

您还需要缩进第一个“测试”,而不是将它直接放在 inital 之后"""\确保多行字符串不以换行符开头)。

回答by cmcginty

You can tell Groovy that the statement should evaluate past the line ending by adding a pair of parentheses ( ... )

您可以通过添加一对括号来告诉 Groovy 该语句应该在行结束后进行评估 ( ... )

def a = ("test"
  + "test"
  + "test")

A second option is to use a backslash, \, at the end of each line:

第二种选择是\在每行末尾使用反斜杠 , :

def a = "test" \
  + "test" \
  + "test"

FWIW, this is identical to how Python multi-line statements work.

FWIW,这与 Python 多行语句的工作方式相同。