Strings
Length (length), case conversion, searching (contains), and replacing (replace).
Simple enough for a 5-year-old to understand
Length and letter case
def s = "HexLibrary"println s.length()println s.toUpperCase()println s.toLowerCase()Length (length), case conversion, searching (contains), and replacing (replace).
Simple enough for a 5-year-old to understand
def s = "HexLibrary"println s.length()println s.toUpperCase()println s.toLowerCase()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.
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.
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
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.
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.
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.
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.