Grails Cookbook - A collection of tutorials and examples

Groovy Substring Examples

Getting a substring from another String is one of the basic building blocks of String manipulation. Most programming languages supports it out of the box. In Groovy, there are some cool ways of how to perform this operation. Below are some examples on different ways of getting substrings in Groovy.

Groovy Substring - Java way

Since Groovy is a superset of Java, any valid Java code is also valid in Groovy. We can also do substring operation in Groovy using the Java String's built-in methods.

There are two variants in the Java String's substring method. The first one takes only one argument, which is the beginning index. The other takes two arguments that represents the beginning and ending index.

substring(int beginIndex)
This method will construct the substring beginning from the given index (inclusive) up to the last character of the String.

Here is the syntax:

public String substring(int beginIndex)
If we have the String "CANDY", the character at index 2 is the "N". If we perform "CANDY".substring(2), the result will be "NDY".

Here are more samples:

def test = "ABCDEFGHIJ";
println test.substring(0)
println test.substring(1)
println test.substring(2)
The output will be:
CANDY
ANDY
NDY

substring(int beginIndex, int endIndex)

This method will construct the substring beginning from the given index (inclusive) up to the given endIndex (exclusive). In short, this can be used to get the middle substring.

Here is the syntax:

public String substring(int beginIndex, int endIndex)
If we have the String "CANDY", the character at index 1 is "A" and the character at index 3 is "D". If we perform "CANDY".substring(1,3), the result will be "AN". Note that endIndex is exclusive, that is why the D was not included.

Here are more examples:

def str = "CANDY"
println str.substring(0,5)
println str.substring(1,4)
println str.substring(2,3)
And this is the output of the code:
CANDY
AND
N

Groovy Substring using take() method

Substring of first n elements

The take() method in Groovy, takes the first n characters of a String and returns it as a substring. For example, if we have the String "CANDY", then invoking "CANDY".take(2) will return "CA because those are the first two characters of the given String.

Here are some more examples:

def str = "CANDY"
println str.take(1)
println str.take(2)
println str.take(3)
println str.take(4)
println str.take(5)
println str.take(6)
And this will be the output:
C
CA
CAN
CAND
CANDY
CANDY
Notice that 6 is greater than available characters of the String "CANDY". In that case, it would just return the whole String.

Substring of last n elements

If we wish to get the last n characters instead of the first n characters, we can do some programming tricks: For example, to get the last two characters of a String, we can do the ff:

def str = "CANDY"
println str.reverse().take(2).reverse()

This will output the last two characters which is:

DY
Here are more examples:
def str = "CANDY"
println str.reverse().take(1).reverse()
println str.reverse().take(2).reverse()
println str.reverse().take(3).reverse()
println str.reverse().take(4).reverse()
println str.reverse().take(5).reverse()
And this is the expected output:
Y
DY
NDY
ANDY
CANDY

Groovy Substring using drop() method

Contrary to take(), the drop method will remove the first n characters and return the resulting String. The effect is that it is equivalent to substring(int beginIndex) in behavior. For example:
def str = "CANDY"
println str.drop(2)
Will remove the first two characters "CA" from "CANDY". The output will be:
NDY
Here are more samples:
def str = "CANDY"
println str.drop(1)
println str.drop(2)
println str.drop(3)
println str.drop(4)
And this is the expected output:
ANDY
NDY
DY
Y


Groovy Substring using index range

Using index range will have similar effect to using substring(int beginIndex, int endIndex) - the only difference is in the treatment of the ending index. This will extract the substring from the beginIndex up to endIndex but inclusive for both bounds.

If we have the String "CANDY", the character at index 1 is the "A" and the character at index 3 is D. If we perform "CANDY"[1..3], the result will be "AND". Note that endIndex is inclusive, which is not the case for "CANDY".substring(1,3) that will result to just "AN".

Here are some examples:

def str = "CANDYBAR"
println str[0..7]
println str[1..6]
println str[2..5]
println str[3..4]
The output will be:
CANDYBAR
ANDYBA
NDYB
DY
Note that the index should be inside the bounds. For example, this code:
def str = "CANDYBAR"
println str[0..8]

Will throw java.lang.StringIndexOutOfBoundsException, because 8 is already out of bounds.

Negative index

It is also possible to use negative numbers for the index. The effect is that it will select from the end of the String moving backwards.

For example, this code:

def str = "CANDYBAR"
println str[0..-4]
Will output:
CANDY
Because the fourth character from the end is Y. So the subtring from beginning of the string up to Y is returned as substring.
Here are other examples:
def str = "CANDYBAR"
println str[-8..-1]
println str[-7..-2]
println str[-6..-3]
println str[-5..-4]
And here is the expected output:
CANDYBAR
ANDYBA
NDYB
DY

Substring of last n elements using index range
We can also use the index range to get the last n characters of a String. For example:
def str = "CANDYBAR"
println str[-2..-1]
This code will print the following:
AR

Which is the last two characters of the String.

Here are additional samples:

def str = "CANDYBAR"
println str[-1..-1]
println str[-2..-1]
println str[-3..-1]
println str[-4..-1]
println str[-5..-1]
println str[-6..-1]
println str[-7..-1]
println str[-8..-1]
And the code will output:
R
AR
BAR
YBAR
DYBAR
NDYBAR
ANDYBAR
CANDYBAR
There could be many more ways on how to perform substring in Groovy, but above are just some possible methods on how to accomplish the operation.