Guava 工具类之 Splitter的使用

Splitter可以对字符串进行分割,在分割时的方式有2种,

  1.按字符/字符串分割

  2.按正则进行分割

Splitter在分割完成时可以转换成listmap

一.按字符进行分割

//1.用指定字符切分字符串,并转换成list
String s1 = "hello|hihi";
String s2 = "hello|haha|||";
Splitter.on("|").splitToList(s1).forEach(System.out::println);
Splitter.on("|").split(s1).forEach(item ->System.out.println(item));

//2.忽略掉空的字符串或者多余的分割符
Splitter.on("|").omitEmptyStrings().splitToList(s2).forEach(System.out::println);
       
 //3.忽略掉字符串中的空格
Splitter.on("|").omitEmptyStrings().trimResults().splitToList("hello | guava|||").forEach(System.out::println);

//4.固定长度分割
Splitter.on("|").fixedLength(4).splitToList("aaaabbbbccccdddd").forEach(System.out::println);


//5.指定长度分割
List<String> list = Splitter.on("#").limit(3).splitToList("a#b#c#d#e#"); //以#来分割,分3部分成 a b #c#d#e 3部分
System.out.println(list.get(0));
System.out.println(list.get(1));
System.out.println(list.get(2));

 

二.按正则来进行分割

//1.传入字符的分割
Splitter.onPattern("\\|").splitToList("hello|world").forEach(System.out::println);
        
//2.传入pattern的分割
Splitter.on(Pattern.compile("\\|")).omitEmptyStrings().trimResults().splitToList("a|b|c||").forEach(System.out::println);

//3.传入pattern 转换成map
  Map<String, String> map = Splitter.on(Pattern.compile("\\|")).omitEmptyStrings()

                       .trimResults().withKeyValueSeparator("=").split("a=b|c=d");

for (Entry<String, String> entry : map.entrySet()) {
  System.out.println(entry.getKey() +" = "+ entry.getValue());
}

 

 三.测试

/**
* list转换为字符串
*/
@Test
public void joinTest(){
  List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
  String result = Joiner.on(",").join(names);

  assertEquals(result, "John,Jane,Adam,Tom");
}


/**
* map转换为字符串
*/
@Test
public void whenConvertMapToString_thenConverted() {
  Map<String, Integer> salary = Maps.newHashMap();
  salary.put("John", 1000);
  salary.put("Jane", 1500);
  String result = Joiner.on(" , ").withKeyValueSeparator(" = ")
    .join(salary);
  System.out.println(result);
}

/**
* list转String,跳过null
*/
@Test
public void whenConvertListToStringAndSkipNull_thenConverted() {
  List<String> names = Lists.newArrayList("John", null, "Jane", "Adam", "Tom");
  String result = Joiner.on(",").skipNulls().join(names);
  System.out.println(result);
  assertEquals(result, "John,Jane,Adam,Tom");
}

/**
* list转String,将null变成其他值
*/
@Test
public void whenUseForNull_thenUsed() {
  List<String> names = Lists.newArrayList("John", null, "Jane", "Adam", "Tom");
  String result = Joiner.on(",").useForNull("nameless").join(names);
  System.out.println(result);
  assertEquals(result, "John,nameless,Jane,Adam,Tom");
}

/**
* String to List
*/
@Test
public void whenCreateListFromString_thenCreated() {
  String input = "apple - banana - orange";
  List<String> result = Splitter.on("-").trimResults().splitToList(input);
  System.out.println(result);
  //assertThat(result, contains("apple", "banana", "orange"));
}

/**
* String to Map
*/
@Test
public void whenCreateMapFromString_thenCreated() {
  String input = "John=first,Adam=second";
    Map<String, String> result = Splitter.on(",")
      .withKeyValueSeparator("=")
      .split(input);

   assertEquals("first", result.get("John"));
    assertEquals("second", result.get("Adam"));
}

/**
* 多个字符进行分割
*/
@Test
public void whenSplitStringOnMultipleSeparator_thenSplit() {
  String input = "apple.banana,,orange,,.";
  List<String> result = Splitter.onPattern("[.|,]")
    .omitEmptyStrings()
    .splitToList(input);
  System.out.println(result);
}

/**
* 每隔多少字符进行分割
*/
@Test
public void whenSplitStringOnSpecificLength_thenSplit() {
  String input = "Hello world";
  List<String> result = Splitter.fixedLength(3).splitToList(input);
  System.out.println(result);
}

/**
* 限制分割多少字后停止
*/
@Test
public void whenLimitSplitting_thenLimited() {
  String input = "a,b,c,d,e";
  List<String> result = Splitter.on(",")
            .limit(4)
          .splitToList(input);

  assertEquals(4, result.size());
  System.out.println(result);
}
posted @ 2019-08-05 14:33  硝烟漫过十八岁  阅读(10327)  评论(0编辑  收藏  举报