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

Stringhe Groovy e GString

Padroneggi la gestione delle stringhe in Groovy: virgolette singole, doppie e triple, interpolazione GString e testo su più righe per le attività di scripting.

Stringhe Groovy e GString è una lezione Groovy & Gradle: JVM Automation and Build Engineering gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Groovy & Gradle: JVM Automation and Build Engineering, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Groovy & Gradle: JVM Automation and Build Engineering include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.

Domande Frequenti

La lezione «Stringhe Groovy e GString» è gratuita?

Sì — il testo completo di «Stringhe Groovy e GString» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Groovy & Gradle: JVM Automation and Build Engineering, passa a CoddyKit PRO. Il corso Groovy & Gradle: JVM Automation and Build Engineering include 4 lezioni in totale.

Cosa imparerò in «Stringhe Groovy e GString»?

Padroneggi la gestione delle stringhe in Groovy: virgolette singole, doppie e triple, interpolazione GString e testo su più righe per le attività di scripting. Eserciti Groovy & Gradle: JVM Automation and Build Engineering con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Groovy & Gradle: JVM Automation and Build Engineering?

Non è richiesta alcuna esperienza precedente. Groovy & Gradle: JVM Automation and Build Engineering su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Stringhe Groovy e GString»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Groovy & Gradle: JVM Automation and Build Engineering?

Sì. Ogni lezione Groovy & Gradle: JVM Automation and Build Engineering include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Introduzione a Groovy e alla JVM
  2. Sintassi e tipi di base di Groovy
  3. Script Groovy e GShell
  4. Stringhe Groovy e GString
← Torna a Groovy & Gradle: JVM Automation and Build Engineering