Groovy single quote triple strings - Should the contained 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 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!''' 

Shouldn't groovy create one long line with single spaces between lines (which means that spaces between lines will not be saved)? Result line:

Join Perl programmers in America's pork producers as we hone our skills and cheer a little ... etc.

In the line I contained all the spaces between the lines. Is this expected behavior?

+4
source share
6 answers

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 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() 
+6
source

Whether on the right, triple quotes do not receive any special treatment, but since you are using groovy, it’s easy enough for you to get the behavior you need:

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

Fingerprints:

 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 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 are looking for additional formatting, you can check out markdown and the MarkdownJ library . I actually just released the Grails Markdown plugin yesterday, which will take text with formatted formatted text and turn it into HTML for GSP.

+5
source

Yes, it was expected. Triple quotes are just a multiline string, there is no magic to detect and remove indentation.

+2
source

To answer the previous answer, follow these steps:

 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 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 use. The backslash (), immediately after which the end of the line (EOL) ends, will be swallowed. (See http://docs.codehaus.org/display/GroovyJSR/Groovy+String+Handling )

+2
source

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

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

or

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

if you just abandoned the formatting requirement and formatted it as

 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 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 line without the need for subsequent processing. It doesn't look too bad imho ...

0
source

All Articles