Cadenas y GStrings de Groovy
Domine el manejo de cadenas en Groovy: comillas simples, dobles y triples, interpolación de GString y texto multilínea para tareas de scripting.
Cadenas y GStrings de Groovy es una lección gratuita de Groovy & Gradle: JVM Automation and Build Engineering en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Groovy & Gradle: JVM Automation and Build Engineering, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Groovy & Gradle: JVM Automation and Build Engineering incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en 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.
Preguntas frecuentes
¿La lección «Cadenas y GStrings de Groovy» es gratis?
Sí — el texto completo de «Cadenas y GStrings de Groovy» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Groovy & Gradle: JVM Automation and Build Engineering, actualiza a CoddyKit PRO. El curso de Groovy & Gradle: JVM Automation and Build Engineering incluye 4 lecciones en total.
¿Qué aprenderé en «Cadenas y GStrings de Groovy»?
Domine el manejo de cadenas en Groovy: comillas simples, dobles y triples, interpolación de GString y texto multilínea para tareas de scripting. Practicas Groovy & Gradle: JVM Automation and Build Engineering con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Groovy & Gradle: JVM Automation and Build Engineering?
No se requiere experiencia previa. Groovy & Gradle: JVM Automation and Build Engineering en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.
¿Cuánto tiempo toma la lección «Cadenas y GStrings de Groovy»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Groovy & Gradle: JVM Automation and Build Engineering?
Sí. Cada lección de Groovy & Gradle: JVM Automation and Build Engineering incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Introducción a Groovy y la JVM
- Sintaxis y tipos básicos de Groovy
- Scripts de Groovy y GShell
- Cadenas y GStrings de Groovy