Below is a sample Java class that split a String to specific length. The example uses Guava API which can be found from this link - https://code.google.com/p/guava-libraries/
import org.apache.hadoop.thirdparty.guava.common.base.Preconditions;
import org.apache.hadoop.thirdparty.guava.common.base.Splitter;
import org.apache.hadoop.thirdparty.guava.common.collect.Iterables;
public class SplitString {
//Main method
public static void main(String[] args) {
String str = "The quick brown fox jumped
over the lazy dog";
int splitLength = 4; //Split to 4 characters
Preconditions.checkArgument(splitLength > 0);
String[] strArray =
Iterables.toArray(
Splitter.fixedLength(splitLength).split(str),
String.class);
//Print to verify
for(String s : strArray){
System.out.println(s);
}
}//End main method
}//End class
No comments:
Post a Comment