Cadeias de Texto Groovy e GStrings
Domine o tratamento de cadeias de texto em Groovy: aspas simples, duplas e triplas, interpolação de GString e texto multilinha para tarefas de scripting.
Cadeias de Texto Groovy e GStrings é uma aula grátis de Groovy & Gradle: JVM Automation and Build Engineering no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Groovy & Gradle: JVM Automation and Build Engineering, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Groovy & Gradle: JVM Automation and Build Engineering inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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 sDouble-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 textTriple Double Quotes
Triple double quotes are multiline and support interpolation — perfect for templated output.
def title = "Report"
def doc = """=== ${title} ===
Generated by Groovy"""
println docSlashy Strings
Slashy strings (between forward slashes) skip backslash escaping — great for regexes and file paths.
def pattern = /\d{4}-\d{2}/
println patternLazy 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 gUseful 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.
Perguntas Frequentes
A aula “Cadeias de Texto Groovy e GStrings” é grátis?
Sim — o texto completo de “Cadeias de Texto Groovy e GStrings” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Groovy & Gradle: JVM Automation and Build Engineering, atualize para CoddyKit PRO. O curso de Groovy & Gradle: JVM Automation and Build Engineering inclui 4 aulas no total.
O que vou aprender em “Cadeias de Texto Groovy e GStrings”?
Domine o tratamento de cadeias de texto em Groovy: aspas simples, duplas e triplas, interpolação de GString e texto multilinha para tarefas de scripting. Você pratica Groovy & Gradle: JVM Automation and Build Engineering com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Groovy & Gradle: JVM Automation and Build Engineering?
Nenhuma experiência prévia é necessária. Groovy & Gradle: JVM Automation and Build Engineering no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “Cadeias de Texto Groovy e GStrings”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Groovy & Gradle: JVM Automation and Build Engineering?
Sim. Cada aula de Groovy & Gradle: JVM Automation and Build Engineering inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Introdução a Groovy e à JVM
- Sintaxe e tipos básicos de Groovy
- Scripts Groovy e GShell
- Cadeias de Texto Groovy e GStrings