Groovy & Gradle: JVM Automation and Build Engineering · Lekcja

Łańcuchy znaków Groovy i GStrings

Opanuj obsługę łańcuchów znaków w Groovy: pojedyncze, podwójne i potrójne cudzysłowy, interpolację GString oraz tekst wieloliniowy na potrzeby skryptów.

Lekcja 4 z 413 kroki

Łańcuchy znaków Groovy i GStrings to bezpłatna lekcja Groovy & Gradle: JVM Automation and Build Engineering na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Groovy & Gradle: JVM Automation and Build Engineering, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Groovy & Gradle: JVM Automation and Build Engineering zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.

Bezpłatny start

Ucz się Groovy dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
12
Lekcje
48

Często zadawane pytania

Czy lekcja „Łańcuchy znaków Groovy i GStrings” jest bezpłatna?

Tak — pełny tekst „Łańcuchy znaków Groovy i GStrings” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Groovy & Gradle: JVM Automation and Build Engineering, przejdź na CoddyKit PRO. Kurs Groovy & Gradle: JVM Automation and Build Engineering zawiera 4 lekcji w sumie.

Co nauczysz się w „Łańcuchy znaków Groovy i GStrings”?

Opanuj obsługę łańcuchów znaków w Groovy: pojedyncze, podwójne i potrójne cudzysłowy, interpolację GString oraz tekst wieloliniowy na potrzeby skryptów. Ćwiczysz Groovy & Gradle: JVM Automation and Build Engineering z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Groovy & Gradle: JVM Automation and Build Engineering?

Nie wymagamy żadnego doświadczenia. Groovy & Gradle: JVM Automation and Build Engineering w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Łańcuchy znaków Groovy i GStrings”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Groovy & Gradle: JVM Automation and Build Engineering?

Tak. Każda lekcja Groovy & Gradle: JVM Automation and Build Engineering zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Wprowadzenie do Groovy i JVM
  2. Składnia Groovy i podstawowe typy
  3. Skrypty Groovy i GShell
  4. Łańcuchy znaków Groovy i GStrings
← Powrót do Groovy & Gradle: JVM Automation and Build Engineering