Groovy String Interpolation
last modified October 15, 2026
String interpolation inserts values and expressions into a string literal. In
Groovy, double-quoted strings with ${} placeholders become
GString objects. This tutorial explains how interpolation works, how
GStrings differ from String, and how to use multi-line and raw
strings.
Basic Interpolation
Place any expression inside ${}. For a simple variable or property,
the shorthand $name also works.
def name = "Bob"
def count = 3
println "Hi, ${name}, you have ${count} messages"
println "Simple: $name"
println "Sum: ${count + 2}, upper: ${name.toUpperCase()}"
Expressions such as count + 2 and method calls such as
toUpperCase() are evaluated inside the placeholder. The shorthand
$name is handy, but use braces when the expression is more than a
plain identifier.
Property and Map Access
Object properties and map entries can be used inside the placeholder with the usual dot notation.
class Book { String title; int year }
def book = new Book(title: "Groovy in Action", year: 2015)
def user = [first: "Ann", last: "Lee"]
println "Book: ${book.title} (${book.year})"
println "Full name: ${user.first} ${user.last}"
Both the object property and the map entries are read directly in the string. This keeps report and message templates short and readable.
Lazy Evaluation with a Closure
A placeholder that contains an expression is evaluated when the GString is
created. To delay evaluation until the string is printed or converted, put an
arrow inside the placeholder: ${-> expression}. The closure is
evaluated each time the GString is converted to text.
def x = 1
def eager = "x is ${x}"
def lazy = "x is ${-> x}"
x = 2
println eager
println lazy
eager captured the value 1 at creation time. The lazy
version reads x when printed, so it shows 2. Lazy
GStrings are useful for logging and templates where the value may change.
GString Versus String
A GString is a different type from String. Groovy's ==
compares the text, so these two values look equal. Java methods such as
equals and collection lookups use the object type and the hash code,
so they treat them as different.
println "a" == "${'a'}"
println "a".equals("${'a'}")
println(["a": 1].containsKey("${'a'}"))
println("${'a'}" as String) == "a"
The first line prints true, but the second and third print
false. Convert a GString to String with
toString() or as String before using it as a map key
or passing it to Java APIs that compare with equals.
Multi-line Strings
Triple single quotes ''' create a multi-line string with no
interpolation. Triple double quotes """ create a multi-line string
that interpolates placeholders.
def name = "Bob"
def raw = '''Raw text: $name stays as written'''
def template = """Dear ${name},
your order has shipped.
Thank you"""
println raw
println template
The single-quoted triple string keeps $name literally. The
double-quoted triple string replaces it with Bob, and it keeps the
line breaks and indentation inside the literal.
Dollar-Slashy Strings
Dollar-slashy strings $/ ... /$ interpolate values but do not treat
backslashes as escape characters. They suit regular expressions and Windows
paths that contain many backslashes.
def folder = "reports"
def path = $/C:\data\${folder}\new\file.txt/$
println path
println $/Quotes "stay" and \n is literal/$
Inside the dollar-slashy string, \n and \d keep their
backslashes, and ${folder} still interpolates. This avoids double
escaping in patterns and paths.
Best Practices
- Convert before comparing: Call
toString()on GStrings before using them as map keys or inequalschecks. - Keep placeholders simple: Move complex logic into a method and call it from the placeholder.
- Choose the right literal: Use
'''for text with literal dollar signs and$/ /$for regular expressions and paths. - Use lazy closures sparingly: Only use
${-> ...}when the value must be read at output time.
Source
Groovy Syntax Documentation | Groovy Semantics Documentation
String interpolation makes Groovy text templates concise. Knowing when a GString
is evaluated and how it differs from String prevents subtle bugs in
map lookups and comparisons.
Author
List all Groovy tutorials.