Joiner.on(separator).join(parts)
返回值为
String
。执行join()之前,必须使用on()设置分隔符。
parts
支持Iterable
、Iterato
、Object[]
以及不常用的String
型不定参。
代码清单1——数组
String[] numberArray = new String[] { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
"Ten" };
String numbers = Joiner.on(";").join(numberArray);
System.out.println(numbers);
//输出:One;Two;Three;Four;Five;Six;Seven;Eight;Nine;Ten
代码清单2——Iterable
Iterable<String> numberIter = Splitter.on(",").split("One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten");
String numbers = Joiner.on(";").join(numberIter);
System.out.println(numbers);
//输出:One;Two;Three;Four;Five;Six;Seven;Eight;Nine;Ten
代码清单3——String不定参
String numbers = Joiner.on(";").join("One", "Two", "Three", "Four");
System.out.println(numbers);
//输出:One;Two;Three;Four
代码清单4——Iterator
String[] numberArray = new String[] { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
"Ten" };
Iterator<String> iterators = Arrays.asList(numberArray).iterator();
String numbers = Joiner.on(";").join(iterators);
System.out.println(numbers);
//输出 One;Two;Three;Four;Five;Six;Seven;Eight;Nine;Ten
Joiner.on(separator).skipNulls().join(parts)
使用
skipNulls()
,可以忽略null
值。避免parts
中有null
值时,抛出NPE异常。
代码清单5
String numbers = Joiner.on(";").join("One","Two","Three",null,"Five");
System.out.println(numbers);
// 抛出异常 java.lang.NullPointerException
代码清单6
String numbers = Joiner.on(";").skipNulls().join("One","Two","Three",null,"Five");
System.out.println(numbers);
//输出:One;Two;Three;Five
Joiner.on(separator).useForNull(nullText).join(parts)
使用
useForNull()
,可以将part中的null值替换为指定字符。
代码清单7
String numbers = Joiner.on(";").useForNull("NULL").join("One", "Two", "Three", null, "Five");
System.out.println(numbers);
//输出:One;Two;Three;NULL;Five
字符串分割 -- Splitter
Splitter.on(separator).split(sequence)
返回值为
Iterable
。执行split()
前,必须使用on()
设置分隔符。
从代码清单8的输出结果可以看出,分割结果并未过滤掉空字符串。
代码清单8
String numberList = "One,Two,Three,Four,Five,,Six,Seven,Eight,Nine,Ten,";
Iterable<String> split = Splitter.on(",").split(numberList);
for (String str : split) {
System.out.print(str + ";");
}
//输出:One;Two;Three;Four;Five;;Six;Seven;Eight;Nine;Ten;;
Splitter.on(separator).omitEmptyStrings().split(sequence)
通过使用
omitEmptyStrings()
,设置Splitter
过滤掉length=0的空字符串
代码清单9
String numberList = "One,Two,Three,Four,Five,,Six,Seven,Eight,Nine,Ten,";
Iterable<String> split = Splitter.on(",").omitEmptyStrings().split(numberList);
for (String str : split) {
System.out.print(str + ";");
}
//输出One;Two;Three;Four;Five;Six;Seven;Eight;Nine;Ten;
字符串分割效率测试
常用字符串分割方法或工具类有:
- JDK : String自带方法
- commons-lang3 : StringUtils工具类
- Google Guave : Splitter工具类
测试代码
/**
* 使用JDK自带方法
*/
public void splitByJdk() {
String numberList = "One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten";
long start = System.currentTimeMillis();
for (int i = 0; i < 2000000; i++) {
numberList.split(",");
}
long end = System.currentTimeMillis();
System.out.println("JDK耗时:" + (end - start));
}
/**
* 使用commons-lang3
*/
public void splitByCommons() {
String numberList = "One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten";
long start = System.currentTimeMillis();
for (int i = 0; i < 2000000; i++) {
StringUtils.split(numberList, ",");
}
long end = System.currentTimeMillis();
System.out.println("commons耗时:" + (end - start));
}
/**
* 使用Google Guava
*/
public void splitByGuava() {
String numberList = "One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten";
long start = System.currentTimeMillis();
for (int i = 0; i < 2000000; i++) {
Splitter.on(",").omitEmptyStrings().split(numberList);
}
long end = System.currentTimeMillis();
System.out.println("guava耗时:" + (end - start));
}
/**
* 使用Google Guava
* 循环外初始化Splitter对象,减少声明
*/
public void splitByGuava2() {
String numberList = "One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten";
long start = System.currentTimeMillis();
Splitter on = Splitter.on(",").omitEmptyStrings();
for (int i = 0; i < 2000000; i++) {
on.split(numberList);
}
long end = System.currentTimeMillis();
System.out.println("guava2耗时:" + (end - start));
}
测试结果