JavaDevNotes.com

Groovy Substring

String manipulation is an important part of any general purpose programming language. For example, we may want to extract some data from a given String or text. In this case, performing Substring is very important. This post will give some ways on how we can extract Subtring in the Groovy language. And since this is Groovy, some are just very cool.

Groovy Take Substring

The Groovy take() method takes the first n elements from the given String. We can see the following code when we check the implementation:

public static CharSequence take(CharSequence self, int num) {
    if (num < 0) {
        return self.subSequence(0, 0);
    }
    if (self.length() <= num) {
        return self;
    }
    return self.subSequence(0, num);
}

And this is how we use the take() method of Groovy to get the first n character substring:

def myString = "JOHN"
println myString.take(2)
This will display below, which is the first two characters of the given String.
JO

Another example:

def myString = "Greatness"
println myString.take(5)

This will display the first 5 characters of Greatness, which is great.
Great
If the parameter is equal or greater than the number of characters in the String, it will just return the original String. Example:
def myString = "Hello"
println myString.take(10)
Will just result in the original String Hello, and the output is shown below:
Great

Groovy Drop Substring

The Groovy drop() method is the inverse of take() method. The take method returns the first n characters, while the drop removes the first n characters and return the remaining characters. It drops the given number of chars from the head of the String. We can see the following code when we check the implementation:

public static CharSequence drop(CharSequence self, int num) {
    if( num <= 0 ) {
        return self ;
    }
    if( self.length() <= num ) {
        return self.subSequence( 0, 0 ) ;
    }
    return self.subSequence(num, self.length()) ;
}

And this is how we use the drop() method of Groovy to remove the first n character substring:

def myString = "JOHN"
println myString.drop(2)
This will display below, which is removing the first two characters of the given String.
HN

Another example:

def myString = "Greatness"
println myString.drop(5)

This will display the last 4 characters of Greatness, which is ness.
ness
If the parameter is equal or greater than the number of characters in the String, it will just return an empty String. Example:
def myString = "Hello"
println myString.drop(10)
This will just output a blank String to the console.

Groovy Range Substring

In groovy, if we know the exact beginning and ending index of the Subtring we want to get, we can just simply specify the range. Since Groovy is a superset of Java, the index should be 0 based. Meaning the first character in the String is at index 0. Below is a simple example:
def myString = "Greatness"
println myString[2..4]
Index 2 is the third character which is e, and 4 is the fifth character which it t. Hence we get the below result that takes all characters from index 2 upto index 4.
eat
This is useful for very specific way of parsing, which is extremely useful. Beware that if the index provided is outside the range, it will throw an Exception. For example:
def myString = "Greatness"
println myString[5..15]
Since there are only nine characters, the index 15 is clearly out of the range. Hence it will result in an exception:
Caught: java.lang.StringIndexOutOfBoundsException: String index out of range: 16
java.lang.StringIndexOutOfBoundsException: String index out of range: 16