Java中为什么尽量使用apache StringUtils.split替代String.splitJava中为什么尽量使用apache StringUtils.split替代String.split

使用apache StringUtils.split替代String.split
如果你对下面几个结果有疑惑的话,建议使用apache commons包的StringUtils.split来替代。
<code>
String[] strs ="".split(",");
<code>

结果是strs.length=1,strs[0]=""

<code>
String[] strs =",".split(",");
<code>

结果是strs.length=0

<code>
String[] strs =",1,".split(",");
<code>

结果是strs.length=2,strs[0]="",strs[1]="1"
String.split使用起来潜规则比较多,即使自己清楚,别人也未必一眼就看明白。为了不引起误会,建议使用StringUtils.split来替代,它对空字符串""会进行过滤。
<code>
String[] strs = StringUtils.split(",1,,2,",",");
<code>

结果是strs.length=2,strs[0]="1",strs[1]="2"

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容