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

6 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!'''
Run Code Online (Sandbox Code Playgroud)

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

加入美国猪肉生产商的Perl程序员,因为我们磨练我们的技能并加以改进......等等

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

Bri*_*arr 8

从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()
Run Code Online (Sandbox Code Playgroud)


Ted*_*eid 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(" ")  
Run Code Online (Sandbox Code Playgroud)

打印:

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!
Run Code Online (Sandbox Code Playgroud)

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


lee*_*tts 2

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