tags:

views:

200

answers:

4

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?

+1  A: 

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

cheers

Lee

leebutts
+3  A: 

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.

Ted Naleid
A: 

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

Chii
+1  A: 

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)

MadJack