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.
This method will construct the substring beginning from the given index (inclusive) up to the last character of the String. Here is the syntax:
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:
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:
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:
Here are more examples:
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:
Here are more samples:
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 endIndexbut 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:
Note that the index should be inside the bounds. For example, this code:
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:
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:
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:
Which is the last two characters of the String. Here are additional samples:
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.