Groovy中的三重单引号字符串 - 结果字符串是否应包含额外的空格?

时间:2009-10-07 02:44:35

标签: grails groovy

如果我使用以下groovy代码:

description: '''Join the Perl programmers of the Pork Producers
                of America as we hone our skills and ham it up
                a bit.  You can show off your programming chops
                while trying to win a year's supply of pork
                chops in our programming challenge.

                Come and join us in historic (and aromatic),
                Austin, Minnesota.  You'll know when you're
                there!'''

不是groovy应该创建一个只有行之间有单个空格的长字符串(意味着不会保留行之间的空格)?结果字符串将是:

加入美国猪肉生产商的Perl程序员,因为我们磨练我们的技能并将其简化......等等。

我收到的字符串包含行之间的所有空格。这是预期的行为吗?

6 个答案:

答案 0 :(得分:7)

自Groovy 1.7.3以来:

def description = '''\
          Join the Perl programmers of the Pork Producers
          of America as we hone our skills and ham it up
          a bit.  You can show off your programming chops
          while trying to win a year's supply of pork
          chops in our programming challenge.

          Come and join us in historic (and aromatic),
          Austin, Minnesota.  You'll know when you're
          there!'''.stripIndent()

答案 1 :(得分:5)

李的权利,三重引用的字符串没有给予任何特殊处理,但因为你使用groovy,它很容易得到你想要的行为:

def description = '''Join the Perl programmers of the Pork Producers
                 of America as we hone our skills and ham it up
                 a bit.  You can show off your programming chops
                 while trying to win a year's supply of pork
                 chops in our programming challenge.

                 Come and join us in historic (and aromatic),
                 Austin, Minnesota.  You'll know when you're
                 there!'''

description.split("\n").collect { it.trim() }.join(" ")  

打印:

Join the Perl programmers of the Pork Producers of America as we hone our skills and ham it up a bit.  You can show off your programming chops while trying to win a year's supply of pork chops in our programming challenge.  Come and join us in historic (and aromatic), Austin, Minnesota.  You'll know when you're there!

如果您正在寻找其他格式,可能需要查看markdown语法和MarkdownJ library。我实际上昨天发布了一个Grails Markdown plugin,它将采用markdown格式的文本并将其转换为HTML格式的GSP。

答案 2 :(得分:2)

是的,这是预期的。三引号只是一个多行字符串,检测和删除缩进没有魔力。

答案 3 :(得分:2)

如果您这样做,可以依赖上一个答案:

def description = '''\
Join the Perl programmers of the Pork Producers
of America as we hone our skills and ham it up
a bit.  You can show off your programming chops
while trying to win a year's supply of pork
chops in our programming challenge.

Come and join us in historic (and aromatic),
Austin, Minnesota.  You'll know when you're
there!'''

文本格式非常易于使用。吞下一个反斜杠()后紧跟行尾(EOL)。 (见http://docs.codehaus.org/display/GroovyJSR/Groovy+String+Handling

答案 4 :(得分:1)

您可以使用正则表达式来消除额外的空格:

description.replaceAll('\\s+', ' ')

(description =~ /\s+/).replaceAll(' ')

答案 5 :(得分:0)

如果您放弃格式化要求,请将其格式化为

description: '''Join the Perl programmers of the Pork Producers
of America as we hone our skills and ham it up
a bit.  You can show off your programming chops
while trying to win a year's supply of pork
chops in our programming challenge.

Come and join us in historic (and aromatic),
Austin, Minnesota.  You'll know when you're
there!'''

然后您将获得所需的字符串,而无需对其进行后期处理。它看起来不太糟糕...