Groovy String Operations

Groovy String tutorial shows how to work with strings in Groovy. Learn to manipulate strings with practical examples.

Groovy String Operations

Groovy String Operations

last modified March 22, 2025

Groovy arms you with a vibrant toolkit for string handling, blending simplicity with power. This tutorial dives into key string operations—concatenation, interpolation, and manipulation—through practical, real-world examples that make text wrangling a breeze.

String Concatenation

In Groovy, strings snap together with the + operator or the concat method, letting you build messages piece by piece.

StringConcatenation.groovy

def user = “Alice” def action = “logged in” def logEntry = user + " " + action

println logEntry

logEntry fuses user and action with a space, crafting a basic log line. Think of this stitching together audit trails in a system monitor—straightforward and reliable.

String Interpolation

Groovy’s ${} interpolation weaves variables right into strings, making dynamic text as smooth as a conversation—no clunky glue required.

StringInterpolation.groovy

def name = “Bob” def welcome = “Hi, ${name}, enjoy your stay!”

println welcome

welcome embeds name seamlessly, personalizing a greeting. Picture this in a hotel app, tailoring check-in messages for guests with zero fuss—elegant and readable.

String Manipulation

Groovy dishes out handy methods like toUpperCase, toLowerCase, and reverse to twist and tweak strings to your liking.

StringManipulation.groovy

def title = “Sale Event”

println title.toUpperCase() println title.toLowerCase() println title.reverse()

title gets reshaped three ways—shouted, whispered, or flipped. This could jazz up a promo banner, adjusting case for emphasis or creating a quirky display twist in a marketing tool.

String Length and Substrings

Groovy lets you measure strings with length and snip out chunks with substring, perfect for dissecting text precisely.

StringLengthSubstring.groovy

def code = “ORDER12345”

println code.length() println code.substring(5, 9)

code reveals its size and yields a numeric slice. Imagine parsing an order ID in an e-commerce system, pulling out key digits for validation or display—neat and tidy.

String Splitting

The split method chops strings into arrays using a delimiter, turning messy text into manageable pieces.

StringSplitting.groovy

def csvLine = “Alice,25,F” def fields = csvLine.split(’,')

fields.each { field -> println field }

csvLine splits into name, age, and gender fields. This mirrors processing a CSV file in a data importer, breaking rows into actionable bits with Groovy’s each for easy iteration.

String Replacement

Groovy’s replace and replaceAll methods swap out string parts, offering quick fixes or pattern-based overhauls.

StringReplacement.groovy

def feedback = “Service was good”

println feedback.replace(“good”, “great”) println feedback.replaceAll(“e”, “3”)

feedback upgrades “good” to “great” or swaps “e” for “3”. This could tweak user reviews in a feedback app or mask text for a playful filter—two sides of Groovy’s replacement coin.

String Padding

Groovy’s padLeft and padRight align strings with padding, handy for formatting fixed-width outputs.

StringPadding.groovy

def id = “42” def paddedId = id.padLeft(5, “0”)

println paddedId

paddedId pads “42” with zeros to five digits. This fits a ticket system, ensuring IDs line up neatly in reports or displays—consistency made simple.

String Joining

Groovy’s join fuses a list into a string with a delimiter, flipping split for seamless assembly.

StringJoining.groovy

def tags = [“news”, “tech”, “groovy”] def tagLine = tags.join(" | “)

println tagLine

tagLine glues tags with " | “, crafting a readable list. Think blog post tags—joining keywords into a sleek, user-friendly string for a sidebar or footer.

String Matching with Regex

Groovy’s regex support via =~ and findAll hunts patterns, pulling matches or testing strings with finesse.

StringRegex.groovy

def text = “Contact: alice@site.com, bob@site.com” def emails = text.findAll(/[\w.-]+@[\w.-]+/)

println emails

emails extracts email addresses with a regex pattern. This could power a contact scraper, harvesting emails from text for a CRM tool—regex magic at work.

Best Practices for Working with Strings

Favor Interpolation: Opt for ${} over

  • to keep code crisp and clear, like a well-edited story. Guard Against Nulls: Use the Elvis operator ?: to dodge null pitfalls—safety first! Tap Regex Power: Lean on regular expressions for intricate text surgery, slicing through complexity with precision. Mind Performance: Watch out for heavy string ops in tight loops—keep it lean for speed.

Source

Groovy String Documentation

This tutorial unpacked Groovy’s string prowess, from stitching and slicing to polishing text with flair. With these tools, you’ll shape strings effortlessly, tackling tasks from logs to user interfaces with Groovy gusto.

Author

My name is Jan Bodnar, and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Groovy tutorials.

ad ad