Introduction
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)
- def test = "ABCDEFGHIJ";
- println test.substring(0)
- println test.substring(1)
- println test.substring(2)
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)
- def str = "CANDY"
- println str.substring(0,5)
- println str.substring(1,4)
- println str.substring(2,3)
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)
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()
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()
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)
Here are more samples:
- def str = "CANDY"
- println str.drop(1)
- println str.drop(2)
- println str.drop(3)
- println str.drop(4)
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:
- def str = "CANDYBAR"
- println str[0..7]
- println str[1..6]
- println str[2..5]
- println str[3..4]
Note that the index should be inside the bounds. For example, this code:
- def str = "CANDYBAR"
- println str[0..8]
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]
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]
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]
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]
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.
沒有留言:
張貼留言