Groovyの文字列とGString
Groovyの文字列操作を使いこなします。シングル、ダブル、トリプルクォート、GString補間、スクリプト作業のための複数行テキストを扱います。
「Groovyの文字列とGString」はCoddyKit上の無料Groovy & Gradle: JVM Automation and Build Engineeringレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはGroovy & Gradle: JVM Automation and Build Engineering学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Groovy & Gradle: JVM Automation and Build Engineeringコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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.
よくある質問
「Groovyの文字列とGString」レッスンは無料ですか?
はい。「Groovyの文字列とGString」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Groovy & Gradle: JVM Automation and Build Engineeringコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Groovy & Gradle: JVM Automation and Build Engineeringコースには全4レッスンが含まれています。
「Groovyの文字列とGString」で何を学びますか?
Groovyの文字列操作を使いこなします。シングル、ダブル、トリプルクォート、GString補間、スクリプト作業のための複数行テキストを扱います。 ブラウザで直接実行するハンズオンコードでGroovy & Gradle: JVM Automation and Build Engineeringを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Groovy & Gradle: JVM Automation and Build Engineeringを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのGroovy & Gradle: JVM Automation and Build Engineeringは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「Groovyの文字列とGString」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このGroovy & Gradle: JVM Automation and Build Engineeringレッスンでコードを書いて実行できますか?
はい。すべてのGroovy & Gradle: JVM Automation and Build Engineeringレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- GroovyとJVMの基礎
- Groovyの構文と基本型
- GroovyスクリプトとGShell
- Groovyの文字列とGString