spell 发表于 2018-11-24 15:05:22

google的guava工具类splitter和apache stringutil对比

一直用的是apache的stringutil工具类,其实google的工具类项目  guava中居然也有字符串的分隔类splitter的,在

  http://code.google.com/p/guava-libraries/中可以下载,其中在老外的

  http://www.javacodegeeks.com/2012/12/guava-splitter-vs-stringutils.html

  这篇文章中进行了stringutil的对比,下面大概总结翻译之:
  首先看两者的用法:
  // Apache
StringUtils...
  String[] tokens1 = StringUtils.split('one,two,three',',');

  // Guava splitter...
  Iterable tokens2 =
Splitter.on(',').split('one,two,three');
  
StringUtils静态类来的,spiltter的语法中则要new对象,但splitter中,一个优点
  是,可以去掉多余的空格等,比如:

  Splitter niceCommaSplitter = Splitter.on(',')

  .omitEmptyString()

  .trimResults();
  这里去掉多余的空格,调用时,比如

  niceCommaSplitter.split('one,, two,three'); //'one','two','three'

  niceCommaSplitter.split('four,five'); //'four','five'
  
这个则比较方便。要注意的是splitter返回的是Iterable,这个和StringUtil
  有点不同。
  
效率方面的对比,作者作了比较:
  


Java代码 http://jackyrong.iteye.com/images/icon_copy.gif http://jackyrong.iteye.com/images/icon_star.pnghttp://jackyrong.iteye.com/images/spinner.gif

[*]final String numberList = 'One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten';
[*]
[*]long start = System.currentTimeMillis();
[*]for(int i=0; i
页: [1]
查看完整版本: google的guava工具类splitter和apache stringutil对比