Fork me on GitHub

groovy-string-extensions

A (very small) collection of Groovy (v2.0+) extensions for String and GString.

Note: The GString methods do not modify the GString instance, and instead return a separate GString instance. The GString methods also do not modify the embedded values.

Extensions Methods

.joinLines()

static String joinLines(String self)
static GString joinLines(GString self)

Provides a method to trim leading and trailing whitespace and join multiple with a single space.

Example:

String s = '''
    This is a multi-line String
    that I want to join into one
    single line.
'''

assert s.joinLines() == 'This is a multi-line String that I want to join into one single line.'

.trimAndCollapseWhitespace()

static String trimAndCollapseWhitespace(String self)
static String trimAndCollapseWhitespace(GString self)

Provides a method to trim leading and trailing whitespace and collapse multiple whitespace characters to one single space.

Example:

GString query = """
    SELECT *
    FROM   cities
    WHERE  country = ${country}
    AND    region  = ${region}
"""

assert query.trimAndCollapseWhitespace() == "SELECT * FROM cities WHERE country = ${country} AND region = ${region}"