开发者

Triple single quote strings in Groovy - Should the resulting string contain extra spaces?

If I use the following groovy code:

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!'''

Isn't groovy supposed to create one long string with only single spaces between lines (meaning that the spaces between lines will not be kept)? The resulting string would be:

Join the Perl programmers of the Pork Producers of America as we hone our skills and ham it up a bit... etc

The string I got contained all the spaces between lines. Is this the expected behavior?


Since 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()


Lee's right, triple quoted strings aren't given any special treatment, but because you're using groovy, it's easy enough to get the behavior you want:

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(" ")  

prints:

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!

If you're looking for additional formatting, you might want to check out markdown syntax and the MarkdownJ library. I actually just released a Grails Markdown plugin yesterday that will take markdown formatted text and turn it into HTML for a GSP.


To piggyback on the previous answer, if you do this:

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!'''

the text format is pretty easy to work with. A backslash () immediately followed by end of line (EOL) will be swallowed. (See http://docs.codehaus.org/display/GroovyJSR/Groovy+String+Handling)


Yes, that's expected. Triple quotes is just a multi-line string, there's no magic to detect and remove indentation.


You can use a regular expression to get rid of the extra white spaces:

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

or

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


if you just forgo the formatting requirement, and format it like

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!'''

then you will get the desired string, without having to post-process it. It doesnt look too bad imho...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜