public static String[] split(String s, String separator)
{
if(s == null)
throw new NullPointerException("source String cannot be null");
if(separator == null)
throw new NullPointerException("separator cannot be null");
if(separator.length() == 0)
throw new IllegalArgumentException("separator cannot be empty");
ArrayList tmp = new ArrayList();
int start = 0;
int separatorLen = separator.length();
int end = s.indexOf(separator);
while(end != -1)
{
tmp.add(s.substring(start, end));
start = end + separatorLen;
end = s.indexOf(separator, start);
}
tmp.add(s.substring(start, s.length()));
String[] result = new String[tmp.size()];
tmp.toArray(result);
return result;
}
public static void main(String[] args)
{
String[] result = split("a,,b,c,,d", ",");
for(int i=0; i<result.length; i++)
System.out.println(result[i]);
}
delimiters (the characters that separate tokens)
StringTokenizer是按delimiters中的每一个字符分解的, 如StringTokenizer st = new StringTokenizer("a3bc1def1gh", "13"); 分解的结果是a,bc,def,gh