2009-10-07 6 views
4

Si j'utilise le code Groovy suivant:Triple guillemets simples dans Groovy - La chaîne résultante doit-elle contenir des espaces supplémentaires?

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 censé ne pas créer une longue chaîne avec seulement des espaces simples entre les lignes (ce qui signifie que les espaces entre les lignes ne seront pas conservés)? La chaîne résultante serait la suivante:

Rejoignez les programmeurs Perl des producteurs de porc de l'Amérique que nous Hone nos compétences et le jambon, il un peu ... etc

La chaîne que je suis contenait tous les espaces entre les lignes. Est-ce le comportement attendu?

Répondre

2

Oui, c'est prévu. Les guillemets triples sont juste une chaîne multiligne, il n'y a pas de magie pour détecter et supprimer l'indentation.

5

Lee de droite, les chaînes entre guillemets triples ne sont pas donnés un traitement spécial, mais parce que vous utilisez groovy, il est assez facile d'obtenir le comportement souhaité:

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

impressions:

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! 

Si vous recherchez une mise en forme supplémentaire, vous souhaiterez peut-être vérifier la syntaxe markdown et le MarkdownJ library. En fait, je viens de publier un Grails Markdown plugin hier qui prendra du texte au format markdown et le transformera en HTML pour un GSP.

0

si vous renoncez simplement l'exigence de mise en forme et le format comme

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

alors vous obtiendrez la chaîne désirée, sans avoir à post-traiter. Il n'a pas l'air trop mal ... IMHO

2

Pour se greffent sur la réponse précédente, si vous faites ceci:

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

le format texte est assez facile à travailler. Un antislash() immédiatement suivi d'une fin de ligne (EOL) sera avalé. (Voir http://docs.codehaus.org/display/GroovyJSR/Groovy+String+Handling)

1

Vous pouvez utiliser une expression régulière pour se débarrasser des espaces blancs supplémentaires:

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

ou

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

Depuis 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() 
Questions connexes