0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · Lesson

Groovy Strings & GStrings

Master Groovy string handling: single, double and triple quotes, GString interpolation, and multiline text for scripting tasks.

Groovy Strings & GStrings is a free Groovy & Gradle: JVM Automation and Build Engineering lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Groovy & Gradle: JVM Automation and Build Engineering learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Strings in Groovy

Groovy gives you several string literals, each for a job. Picking the right one keeps scripts clean and dodges escaping headaches.

Single-Quoted Strings

Single quotes make a plain String with no interpolation — what you type is exactly what you get.

def s = 'Hello World'
println s

Double-Quoted GStrings

Double quotes make a GString: any expression inside ${...} is evaluated and dropped into the text.

def name = "Alice"
println "Hello, ${name}!"

Simplified Interpolation

For a single variable you can skip the braces and just put a dollar sign before the name.

def user = "Bob"
println "Welcome $user"

Expressions Inside GStrings

Any expression fits inside ${...} — method calls, arithmetic, whatever you need.

def a = 3, b = 4
println "Sum is ${a + b}"

Triple-Quoted Strings

Triple single quotes give a multiline plain string — ideal for text blocks without escaping every newline.

def text = '''Line one
Line two
Line three'''
println text

Triple Double Quotes

Triple double quotes are multiline and support interpolation — perfect for templated output.

def title = "Report"
def doc = """=== ${title} ===
Generated by Groovy"""
println doc

Slashy Strings

Slashy strings (between forward slashes) skip backslash escaping — great for regexes and file paths.

def pattern = /\d{4}-\d{2}/
println pattern

Lazy GString Evaluation

A GString can embed a closure (${-> ...}) that re-evaluates on each render, capturing live values instead of a snapshot.

def count = 1
def g = "Count: ${-> count}"
count = 2
println g

Useful String Methods

Groovy adds handy string methods like padLeft and reverse, plus easy slicing with index ranges.

def s = "Groovy"
println s.reverse()
println s[0..2]

GString vs String Equality

A GString isn't a String, but they compare equal and auto-convert. Call toString() when the exact type matters, like map keys.

def k = "id_${1}".toString()
def m = [(k): "value"]
println m["id_1"]

Quick Check

Test your Groovy string knowledge.

Recap

You mastered Groovy strings: plain vs GString, triple-quoted multiline, slashy strings for regex, and lazy ${-> ...} closures.

Frequently asked questions

Is the “Groovy Strings & GStrings” lesson free?

Yes — the full text of “Groovy Strings & GStrings” is free to read here on the web, and the Groovy & Gradle: JVM Automation and Build Engineering course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Groovy & Gradle: JVM Automation and Build Engineering course, upgrade to CoddyKit PRO.

What will I learn in “Groovy Strings & GStrings”?

Master Groovy string handling: single, double and triple quotes, GString interpolation, and multiline text for scripting tasks. You practise Groovy & Gradle: JVM Automation and Build Engineering with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Groovy & Gradle: JVM Automation and Build Engineering?

No prior experience is required. Groovy & Gradle: JVM Automation and Build Engineering on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Groovy Strings & GStrings” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Groovy & Gradle: JVM Automation and Build Engineering lesson?

Yes. Every Groovy & Gradle: JVM Automation and Build Engineering lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Introduction to Groovy & JVM
  2. Groovy Syntax & Basic Types
  3. Groovy Scripts & GShell
  4. Groovy Strings & GStrings
← Back to Groovy & Gradle: JVM Automation and Build Engineering