What's wrong with Groovy multi-line String?

后端 未结 3 893
醉酒成梦
醉酒成梦 2020-12-02 16:31

Groovy scripts raises an error:

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

Error:

No signature of method: java.lang.String.         


        
3条回答
  •  忘掉有多难
    2020-12-02 16:47

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

    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).

提交回复
热议问题