Grails Groovy中的三重单引号字符串-结果字符串是否应该包含额外的空格?
如果我使用以下groovy代码:Grails Groovy中的三重单引号字符串-结果字符串是否应该包含额外的空格?,grails,groovy,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
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程序员,我们将磨练我们的技能,并稍微提高一点。。。等
我得到的字符串包含所有行之间的空格。这是预期的行为吗?是的,这是预期的。三重引号只是一个多行字符串,检测和删除缩进没有什么魔力。Lee是对的,三重引号字符串没有得到任何特殊处理,但是因为您使用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!
如果要查找其他格式,可能需要检查语法和。事实上,我昨天刚刚发布了一个新版本,它将采用降价格式的文本,并将其转换为GSP的HTML。如果您放弃格式要求,并将其格式化为
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!'''
然后您将获得所需的字符串,而无需对其进行后期处理。如果你这样做的话,背上前面的答案看起来并不太糟糕
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)的反斜杠()将被删除。(请参见)您可以使用正则表达式来消除多余的空格:
description.replaceAll('\\s+', ' ')
或
自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()
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()