Common String
manipulation routines.
Originally from * Turbine and the * GenerationJavaCore library.
* * @author Jon S. Stevens * @author Daniel Rall * @author Greg Coladonato * @author Henri Yandell * @author Ed Korthof * @author Rand McNeely * @author Stephen Colebourne * @author Fredrik Westermarck * @version $Id$ */ public class StringUtil { /** * Splits the provided text into a list, based on a given separator. * The separator is not included in the returned String array. * The maximum number of splits to perfom can be controlled. * A null separator will cause parsing to be on whitespace. * *This is useful for quickly splitting a string directly into
* an array of tokens, instead of an enumeration of tokens (as
* StringTokenizer
does).
*
* @param str The string to parse.
* @param separator Characters used as the delimiters. If
* null
, splits on whitespace.
* @param max The maximum number of elements to include in the
* list. A zero or negative value implies no limit.
* @return an array of parsed Strings
*/
public static String[] split(String str, String separator, int max) {
StringTokenizer tok = null;
if (separator == null) {
// Null separator means we're using StringTokenizer's default
// delimiter, which comprises all whitespace characters.
tok = new StringTokenizer(str);
}
else {
tok = new StringTokenizer(str, separator);
}
int listSize = tok.countTokens();
if (max > 0 && listSize > max) {
listSize = max;
}
String[] list = new String[listSize];
int i = 0;
int lastTokenBegin = 0;
int lastTokenEnd = 0;
while (tok.hasMoreTokens() ) {
if (max > 0 && i == listSize - 1) {
// In the situation where we hit the max yet have
// tokens left over in our input, the last list
// element gets all remaining text.
String endToken = tok.nextToken();
lastTokenBegin = str.indexOf(endToken, lastTokenEnd);
list[i] = str.substring(lastTokenBegin);
break;
}
else {
list[i] = tok.nextToken();
lastTokenBegin = str.indexOf(list[i], lastTokenEnd);
lastTokenEnd = lastTokenBegin + list[i].length();
}
i++;
}
return list;
}
}