Groovy Strings: Declaration, Methods, and Operators

String Definition Syntax

Groovy provides three distinct ways to define a string literal, each with difefrent behavior regarding escaping, formatting, and expansion.

Single‑Quoted Strings

A string enclosed in single quotes behaves much like a Java String. Escape sequences must be explicit.

package com.example.demo

def s1 = 'hello groovy\nworld'
println s1
println s1.class // java.lang.String

Double‑Quoted Strings (GStrings)

Double quotes create a GString when expression are present. This alows embedding variables and expressions using ${...}.

package com.example.demo

def s2 = "hello groovy2"
println s2
println s2.class // java.lang.String (no interpolation)

def base = "Groovy"
def s3 = "hello ${base}"
println s3           // hello Groovy
println s3.class     // org.codehaus.groovy.runtime.GStringImpl

def s4 = "100 + 100 = ${100 + 100}"
println s4
println s4.class

// GString can be used wherever String is expected
String convert(String str) { str }
def result = convert(s3)
println result
println result.class   // java.lang.String

Triple‑Quoted Strings

Triple quotes (both single and double variants) preserve line breaks and internal formatting. A leading backslash after the opening quotes trims the first newline, aligning the output with the source layout.

package com.example.demo

def s5 = '''hello groovy3'''
println s5
println s5.class // java.lang.String

def s6 = '''\
hi
multiline'''
println s6

// Triple double quotes also support interpolation
def name = "Groovy"
def s7 = """\
Dear $name,
Welcome to the tutorial.
"""
println s7

Common String Operations

Standard Java Methods

All methods from java.lang.String are directly available.

package com.example.demo

def text = "apachegroovy"
println "Length: ${text.length()}"
println "Empty? ${text.isEmpty()}"
println "Char at 2: ${text.charAt(2)}"

def other = "apachegroovy"
println "Equals: ${text.equals(other)}"
println "Substring(3): ${text.substring(3)}"
println "Substring(2,4): ${text.substring(2,4)}"
println "Replace 'o' with 'O': ${text.replace('o', 'O')}"

def csv = "x-y-z-11-22"
def parts = csv.split("-")
println "Split into array: $parts"

def caseDemo = "sampleText"
println "Uppercase: ${caseDemo.toUpperCase()}"
println "Lowercase: ${caseDemo.toUpperCase().toLowerCase()}"

def padded = "  centered  "
println "Trimmed: '${padded.trim()}'"

def a = "x"
def b = "y"
println "compareTo: ${a.compareTo(b)}"

Groovy‑Enhanced Methods

The StringGroovyMethods class adds a rich set of utilities.

package com.example.demo

def core = "groovy"
println "Center with spaces: '${core.center(12)}'"
println "Center with '#': '${core.center(12,'#')}'"

println "Pad left: '${core.padLeft(10,'.')}'"

def strA = "learninggroovy"
def strB = "groovy"
println "Subtract: ${strA.minus(strB)}"

def revDemo = "live"
println "Reversed: ${revDemo.reverse()}"

println "Capitalize: ${revDemo.capitalize()}"

def numberStr = "246"
println "To Integer: ${numberStr.toInteger().class}"

Groovy Operators for Strings

Groovy overloads several operators to simplify string manipulation.

def a = "a"
def b = "b"
println a > b   // false

def lang = "groovy"
println lang[1]         // r
println lang[2..3]      // oo

def left = "helloJava"
def right = "Java"
println left - right    // hello

// Repeat operator
println "ab" * 3        // ababab

Tags: Groovy string programming

Posted on Sun, 10 May 2026 03:23:25 +0000 by RyanW