java内置的会忽略空内容,guava Splitter不会忽略.或者可以自由选择的忽略.
/**
* {@code Splitter}和{@code Joiner} 这两个对于map的相反的操作,在web框架例如spring mvc中用到过.
*
* 对于get url后面的参数和pos form中前端参数的连接和后端的拆分都有用到.每个框架的处理思维是一样的.
*
* @author doctor
*
* @since 2015年3月14日 下午9:00:17
*/
..............
@Test
public void test_string_split() {
String[] split = "a,b,,".split(","); // java内置的会忽略空内容
assertThat(split, equalTo(Lists.newArrayList("a", "b").toArray()));
}
@Test
public void test_guava_Splitter() {
List<String> splitToList = Splitter.on(",").splitToList("a,b,,");//guava不会忽略
assertThat(splitToList, equalTo(Lists.newArrayList("a", "b", "", "")));
}
@Test
public void test_guava_Splitter_omitEmptyStrings() {
// 或者可以自由选择的忽略 ,像java一样,忽略空字符串
List<String> splitToList = Splitter.on(",").omitEmptyStrings().trimResults().splitToList("a,b,,");
assertThat(splitToList, equalTo(Lists.newArrayList("a", "b")));
}
时间: 2024-11-23 11:49:18