程式扎記: [ 文章收集 ] Groovy Substring Examples

標籤

2016年3月15日 星期二

[ 文章收集 ] Groovy Substring Examples

Source From Here 
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: 
  1. 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: 
  1. def test = "ABCDEFGHIJ";  
  2. println test.substring(0)  
  3. println test.substring(1)  
  4. 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: 
  1. 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: 
  1. def str = "CANDY"  
  2. println str.substring(0,5)  
  3. println str.substring(1,4)  
  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: 
  1. def str = "CANDY"  
  2. println str.take(1)  
  3. println str.take(2)  
  4. println str.take(3)  
  5. println str.take(4)  
  6. println str.take(5)  
  7. 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: 
  1. def str = "CANDY"  
  2. println str.reverse().take(2).reverse()  
This will output the last two characters which is: 
DY

Here are more examples: 
  1. def str = "CANDY"  
  2. println str.reverse().take(1).reverse()  
  3. println str.reverse().take(2).reverse()  
  4. println str.reverse().take(3).reverse()  
  5. println str.reverse().take(4).reverse()  
  6. 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: 
  1. def str = "CANDY"  
  2. println str.drop(2)  
Will remove the first two characters "CA" from "CANDY". The output will be: 
NDY

Here are more samples: 
  1. def str = "CANDY"  
  2. println str.drop(1)  
  3. println str.drop(2)  
  4. println str.drop(3)  
  5. 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 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: 
  1. def str = "CANDYBAR"  
  2. println str[0..7]  
  3. println str[1..6]  
  4. println str[2..5]  
  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: 
  1. def str = "CANDYBAR"  
  2. 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: 
  1. def str = "CANDYBAR"  
  2. 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: 
  1. def str = "CANDYBAR"  
  2. println str[-8..-1]  
  3. println str[-7..-2]  
  4. println str[-6..-3]  
  5. 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: 
  1. def str = "CANDYBAR"  
  2. println str[-2..-1]  
This code will print the following: 
AR

Which is the last two characters of the String. Here are additional samples: 
  1. def str = "CANDYBAR"  
  2. println str[-1..-1]  
  3. println str[-2..-1]  
  4. println str[-3..-1]  
  5. println str[-4..-1]  
  6. println str[-5..-1]  
  7. println str[-6..-1]  
  8. println str[-7..-1]  
  9. 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.

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!