`
Surmounting
  • 浏览: 62999 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
实测比 String 的 split() 方法快 3 倍以上的字符串简易分割方法 字符串, 分割, 分隔, 高效, 快速, split
/**
 * 对于字符串分隔,在 Java 6.0 以内,都只提供了支持正则表达式的 split() 一套方法。
 * 对于简单的分割,正则表达式消耗了一半以上的运算资源。先考虑大规模计算中频繁用
 * 到的字符串分割,自制了一个方法,通过取出 char[] ,逐一判断是否是分割用的字符,
 * 通过 {@link System#arraycopy(Object, int, Object, int, int)} 取出一个个字符
 * 数组,生成字符串,存放进一个 {@link LinkedList} 里边。如果规模比较大,拟考虑
 * 通过二重循环,用 String[] 作为结果收集器。
 */
static public List<String> splitSimpleString(String source, char gap)
{
	List<String> result = new LinkedList<String>();
	if (source == null) return result;
	char[] sourceChars = source.toCharArray();
	int startIndex = 0, index = -1;
	while (index++ != sourceChars.length)
	{
		if (index == sourceChars.length || sourceChars[index] == gap)
		{
			char[] section = new char[index - startIndex];
			System.arraycopy(sourceChars, startIndex,
					section, 0, index - startIndex);
			result.add(String.valueOf(section));
			startIndex = index + 1;
		}
	}
	return result;
}
Global site tag (gtag.js) - Google Analytics