Search

Dark theme | Light theme

June 20, 2010

Groovy Goodness: Strip Leading Spaces from Lines with Margin

Since Groovy 1.7.3 we can use the stripMargin() method to strip characters up to and including a margin character from multiline strings. The default character is the pipe symbol (|), but we can pass a parameter to the method and use a custom character.

def s = '''\
        |Groovy
        |Grails
        |Griffon'''
        
assert '''\
Groovy
Grails
Griffon''' == s.stripMargin()

def s1 = '''\
   * Gradle
   * GPars
   * Spock''' 
   
assert '''\
 Gradle
 GPars
 Spock''' == s1.stripMargin("* ")