您的位置:首页 > 其它

一个比String.split分割速度更快的分割方法

2013-01-02 16:11 477 查看
public static String[] split(String s, String token) {
if (s == null)
return null;
if (token == null || s.length() == 0)
return new String[] { s };
int size = 0;
String[] result = new String[4];
while (s.length() > 0) {
int index = s.indexOf(token);
String splitOne = s;
if (index > -1) {
splitOne = s.substring(0, index);
s = s.substring(index + token.length());
} else {
s = "";
}
if (size >= result.length) {
String[] tmp = new String[result.length * 2];
System.arraycopy(result, 0, tmp, 0, result.length);
result = tmp;
}
if (splitOne.length() > 0) {
result[size++] = splitOne;
}
}
String[] tmp = result;
result = new String[size];
System.arraycopy(tmp, 0, result, 0, size);
return result;
}


  测试过,这个确实比官方的分割方法速度快,原始作者不晓的是谁了,因为隔好久了,现在总结才找到的代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: