// 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