HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to GroovyVariablesData TypesOperatorsStringsListsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Map
🔀 Condition & Loop
🧩 Function
Groovy›Strings

Strings

Length (length), case conversion, searching (contains), and replacing (replace).

Simple enough for a 5-year-old to understand

Length and letter case

Example code
1
def s = "HexLibrary"
2
println s.length()
10
Output / Input10
3
println s.toUpperCase()
HEXLIBRARY
Output / InputHEXLIBRARY
4
println s.toLowerCase()
hexlibrary
Output / Inputhexlibrary
Output / Input
💡 Hint: Concatenation is done with + or with "$a$b" interpolation.

About this topic

In Groovy, quote types carry meaning. Single quotes produce a plain string; double quotes produce a string with template support, where expressions inside are replaced with their values.

Triple quotes provide multi-line strings, and template support still depends on the quote type used. There is also a slashy string format; it is very useful in regular expressions because it eliminates the clutter of backslashes.

Templated strings are actually a separate type and convert to normal strings when needed. This distinction is mostly invisible, but using one as a map key can lead to unexpected behavior.

What you will learn

  • The difference between quote types
  • Template syntax
  • Writing multi-line strings
  • Slashy string format
  • String functions

Who is this for?

  • Developers generating script output
  • Those expecting templates with single quotes
  • Those writing regular expressions

When to use?

In build scripts, file paths and command lines are generated using string templates; choosing the right quote type prevents errors.

Using the slashy format in regular expressions cuts backslash escapes in half and makes the expression readable.

Common mistakes

Wrong

def liste = [1, 2, 3]
println "Count: $liste.size()"

The dollar sign only takes a simple property chain; it does not cover method calls and produces unexpected output. If you want to write an expression, you need to use curly braces.

Correct

def liste = [1, 2, 3]
println "Count: ${liste.size()}"

Wrong

def desen = "\\d+"
println "abc123" =~ desen

In normal strings, backslashes must be written twice and the expression quickly becomes unreadable. Groovy's slashy string format makes these escapes unnecessary, making it the standard choice for regular expressions.

Correct

def desen = /\d+/
println "abc123" =~ desen

Wrong

def metin = "hello"
metin.toUpperCase()
println metin

Since strings are immutable, the method does not change the original, it returns a new string. Since you didn't assign the result, the lowercase version is still printed to the screen. Ignoring the return value is a silent error and produces no warnings.

Correct

def metin = "hello"
metin = metin.toUpperCase()
println metin

Frequently asked questions

Which quote should I use and when?

Single quotes if there are no variables; double quotes if template support is needed. Triple quotes are used for multi-line strings, and the slashy format for regular expressions. This choice both clarifies intent and prevents unnecessary operations.

Is the difference between a templated string and a normal string important?

In most cases no, because conversion happens automatically when needed. However, when used as a map key, the two types can behave differently; explicitly converting to a string in such places is safe.

How are search and replace performed within a string?

With regular expression operators. There are separate operators for matching tests and matcher generation, while replacement is done with the string object's own method and accepts a regular expression as a pattern.

Summary

Single quotes do not support templates.

Double quotes provide template support.

Curly braces are required to write an expression.

The slashy format is for regular expressions.

String methods return a new value.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code