splitByWholeSeparatorPreserveAllTokens 和 split 的区别 [英] Difference between splitByWholeSeparatorPreserveAllTokens and split

查看:58
本文介绍了splitByWholeSeparatorPreserveAllTokens 和 split 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

StringUtils.splitByWholeSeparatorPreserveAllTokens()String.split() 有什么区别?

使用 splitByWholeSeparatorPreserveAllTokens,我们可以限制数组中返回的参数数量.这是唯一的区别吗?

解决方案

java.lang.String.split();
用法:此方法返回的数组包含此字符串的每个子字符串,这些子字符串由与给定表达式匹配的另一个子字符串终止或由字符串的末尾终止.数组中的子字符串按它们在此字符串中出现的顺序排列.如果表达式不匹配输入的任何部分,则结果数组只有一个元素,即这个字符串.

org.apache.commons.lang.StringUtils.splitPreserveAllTokens();
用法:将提供的文本拆分为数组,指定分隔符,保留所有标记,包括由相邻分隔符创建的空标记.这是使用 StringTokenizer 的替代方法.

阅读更多:kickjava_src_apache_StringUtils

String.split() 使用最终的类 Pattern 进行拆分.

Pattern.compile(regex).split(this , limit);

在 StringUtils 中使用 splitWorker(String str, char separatorChar, boolean preserveAllTokens) ,它是自己的方法,这是 2.0 (JDK1.4) 的性能调优.

What is the difference between StringUtils.splitByWholeSeparatorPreserveAllTokens() and String.split()?

With splitByWholeSeparatorPreserveAllTokens, we could limit the number of parameters that are returned in an array. Is this the only difference?

解决方案

java.lang.String.split();
Usage: The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.

org.apache.commons.lang.StringUtils.splitPreserveAllTokens();
Usage: Splits the provided text into an array, separator specified, preserving all tokens, including empty tokens created by adjacent separators. This is an alternative to using StringTokenizer.

Read more: kickjava_src_apache_StringUtils

and String.split() uses the final class Pattern to split.

Pattern.compile(regex).split(this , limit);

in StringUtils uses splitWorker(String str, char separatorChar, boolean preserveAllTokens) , it's own method, which is a Performance tune for 2.0 (JDK1.4).

这篇关于splitByWholeSeparatorPreserveAllTokens 和 split 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆