Integration work is full of text: JSON payloads, file names, log messages, SQL. Groovy makes building and picking apart text much easier than Java.
Single quotes vs double quotes
def customer = 'Acme'
println "Customer $customer owes ${amount * 1.2}" // GString: interpolated
println 'Customer $customer' // plain String: printed as-is
Double quotes create a GString: $variable or ${expression} is replaced by its value. Use single quotes when you don’t need interpolation (and when the text contains $).
Watch out: A GString isn’t a java.lang.String. Where a Java API needs a real String — map keys, Properties.setProperty, some Boomi calls — call .toString() on it.
Multi-line strings
def json = """{
"customer": "$customer",
"amount": $amount
}"""
Triple single quotes '''…''' give multi-line text without interpolation. stripIndent() removes the common leading spaces so code can stay nicely indented.
Slashy strings and regular expressions
Backslashes don’t need escaping inside /…/, which makes regular expressions readable:
def skuPattern = /[A-Z]{2}-\d{3}/
'AX-200' ==~ skuPattern // true: the WHOLE string matches
('Order AX-200 and BX-310' =~ skuPattern).findAll() // [AX-200, BX-310]: find inside
==~— does the whole string match? (boolean)=~— a matcher for finding occurrences.
Methods Groovy adds to String
'po-4471'.toUpperCase().padLeft(10, '*') // ***PO-4471
'a,b,,c'.tokenize(',') // [a, b, c] (split() would keep the empty one)
'hello world'.capitalize() // Hello world
'42'.isInteger() ? '42'.toInteger() : 0 // 42
'Boomi'[0..2] // Boo — ranges as indexes
'Boomi'[-1] // i — negative index counts from the end
'ab' * 3 // ababab
'Acme Ltd' - ' Ltd' // Acme — remove the first occurrence
The full example
// strings.groovy — single quotes, GStrings, multi-line strings and string methods.
def customer = 'Acme' // plain String
def amount = 204.5
// GString: double quotes with ${...} or $variable
println "Customer $customer owes ${amount * 1.2}"
// Single quotes never interpolate
println 'Customer $customer owes ${amount}'
// Triple quotes: multi-line
def json = """{
"customer": "$customer",
"amount": $amount
}"""
println json
// Slashy strings: great for regular expressions (no double escaping)
def skuPattern = /[A-Z]{2}-\d{3}/
println 'AX-200' ==~ skuPattern // true: whole-string match
def found = ('Order AX-200 and BX-310' =~ skuPattern).findAll()
println found // [AX-200, BX-310]
// Handy String methods added by Groovy
println 'po-4471'.toUpperCase().padLeft(10, '*') // ***PO-4471
println 'a,b,,c'.tokenize(',') // [a, b, c] (split keeps empties)
println 'hello world'.capitalize() // Hello world
println '42'.isInteger() ? '42'.toInteger() + 1 : 'not a number' // 43
println 'Boomi'[0..2] // Boo
println 'Boomi'[-1] // i
println 'ab' * 3 // ababab
println 'Acme Ltd' - ' Ltd' // Acme
// Multi-line text with stripIndent
def message = '''\
Dear customer,
your order has shipped.
'''.stripIndent()
print message
Try it yourself
Given def line = 'PO-4471|Acme Ltd|24|8.50', print Acme Ltd ordered 24 × PO-4471 = 204.00 using tokenize and a GString.
Show a solution
def line = 'PO-4471|Acme Ltd|24|8.50'
def (id, customer, qty, price) = line.tokenize('|')
println "$customer ordered $qty × $id = ${qty.toInteger() * price.toBigDecimal()}"
Key takeaways
- Double quotes interpolate (
$x,${expr}); single quotes don’t. - Triple quotes for multi-line text; slashy strings for regex.
==~matches a whole string,=~finds within it.- Groovy adds many helpers:
tokenize,padLeft, ranges as indexes,*and-.