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

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;
	}

  测试过,这个确实比官方的分割方法速度快,原始作者不晓的是谁了,因为隔好久了,现在总结才找到的代码

posted @ 2013-01-02 16:11  杨桃  阅读(735)  评论(0编辑  收藏  举报