Sunday, December 15, 2013
[Java] Splitting String using Guava API
The method below accepts the following parameters:
1. splitLen - number of characters the string will be splitted.
2. arrayLen - new size of a return array.
3. str - string to be splitted.
public static String[] splitString(int splitLen, int arrayLen, String str)
{
String[] returnTokens = null;
String[] tokens = null;
Preconditions.checkArgument(splitLen > 0);
tokens =
Iterables.toArray(
Splitter
.fixedLength(splitLen)
.split(str),
String.class
);
returnTokens = Arrays.copyOf(tokens, arrayLen);
return returnTokens;
}//end method
This is how to call the method.
String str = "Please split the string into 5 characters each but return only the first 3.";
String[] splittedStr = splitString(5,3,str);
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment