Guava入门第二章(Splitter)
Splitter详细介绍
package com.lvshihao.guava;
import com.google.common.base.Splitter;
import org.junit.Test;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertThat;
/**
*@author: LVSHIHAO
*@description: GUAVA Splitter detailed introduction
*/
public class SplitterTest {
@Test
public void testSplitOnSplit(){
List<String> result = Splitter.on("|").splitToList("hello|lvshihao");
assertThat(result,notNullValue());
assertThat(result.size(),equalTo(2));
assertThat(result.get(0),equalTo("hello"));
assertThat(result.get(1),equalTo("lvshihao"));
}
@Test
public void testSplitOnSplitOmitEmpty(){
/**
* omitEmptyStrings()忽略掉空的字符串 ignore Empty Strings
*/
List<String> result = Splitter.on("|").omitEmptyStrings().splitToList("hello|lvshihao|||");
assertThat(result,notNullValue());
assertThat(result.size(),equalTo(2));
assertThat(result.get(0),equalTo("hello"));
assertThat(result.get(1),equalTo("lvshihao"));
}
@Test
public void testSplitOnSplitOmitEmptyTrimResult(){
/**
* trimResults()去除字符串前后的空格 remove Strings first last space
*/
List<String> result = Splitter.on("|").trimResults().omitEmptyStrings().splitToList("hello | lvshihao |||");
assertThat(result,notNullValue());
assertThat(result.size(),equalTo(2));
assertThat(result.get(0),equalTo("hello"));
assertThat(result.get(1),equalTo("lvshihao"));
}
@Test
public void testSplitFixLength(){
/**
* fixedLength()以固定长度截取字符串 with fixed length split strings
*/
List<String> result = Splitter.fixedLength(4).splitToList("aaaabbbbccccdddd");
assertThat(result,notNullValue());
assertThat(result.size(),equalTo(4));
assertThat(result.get(0),equalTo("aaaa"));
assertThat(result.get(1),equalTo("bbbb"));
assertThat(result.get(2),equalTo("cccc"));
assertThat(result.get(3),equalTo("dddd"));
}
@Test
public void testSplitOnSplitLimit(){
/**
* limit() 限制截取几个元素 limit split some element
*/
List<String> result = Splitter.on("#").limit(4).splitToList("hello#lvshihao#lvbu#java#scala");
assertThat(result,notNullValue());
assertThat(result.size(),equalTo(4));
assertThat(result.get(0),equalTo("hello"));
assertThat(result.get(1),equalTo("lvshihao"));
assertThat(result.get(2),equalTo("lvbu"));
assertThat(result.get(3),equalTo("java#scala"));
}
@Test
public void testSplitOnPatternString(){
/**
* onPattern() 使用正则表达式进行切分 use Regular expression conduct split
*/
List<String> result = Splitter.onPattern("\\|").trimResults().omitEmptyStrings().splitToList("hello | lvshihao |||");
assertThat(result,notNullValue());
assertThat(result.size(),equalTo(2));
assertThat(result.get(0),equalTo("hello"));
assertThat(result.get(1),equalTo("lvshihao"));
}
@Test
public void testSplitOnPattern(){
/**
* on() 也可以在on里面使用正则表达式 possible in on inside Regular expression
*/
List<String> result = Splitter.on(Pattern.compile("\\|")).trimResults().omitEmptyStrings().splitToList("hello | lvshihao |||");
assertThat(result,notNullValue());
assertThat(result.size(),equalTo(2));
assertThat(result.get(0),equalTo("hello"));
assertThat(result.get(1),equalTo("lvshihao"));
}
@Test
public void testSplitOnSplitToMap(){
/**
* withKeyValueSeparator() 键值对分割符号,代表这是个键值对类型的 key value pair split sign,represent this is key value type
*/
Map<String, String> result = Splitter.on(Pattern.compile("\\|")).trimResults().omitEmptyStrings().withKeyValueSeparator(":").split("name:lvshihao | age:18 |||");
assertThat(result,notNullValue());
assertThat(result.size(),equalTo(2));
assertThat(result.get("name"),equalTo("lvshihao"));
assertThat(result.get("age"),equalTo("18"));
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
作者:吕世昊
个性签名:学习如逆水行舟,不进则退!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!