Guava⼯具类之Splitter的使⽤Splitter可以对字符串进⾏分割,在分割时的⽅式有2种,
  1.按字符/字符串分割
  2.按正则进⾏分割
Splitter在分割完成时可以转换成和
⼀.按字符进⾏分割
//1.⽤指定字符切分字符串,并转换成list
String s1 = "hello|hihi";
String s2 = "hello|haha|||";
<("|").splitToList(s1).forEach(System.out::println);
<("|").split(s1).forEach(item ->System.out.println(item));
//2.忽略掉空的字符串或者多余的分割符
<("|").omitEmptyStrings().splitToList(s2).forEach(System.out::println);
//3.忽略掉字符串中的空格
<("|").omitEmptyStrings().trimResults().splitToList("hello | guava|||").forEach(System.out::println);
//4.固定长度分割
<("|").fixedLength(4).splitToList("aaaabbbbccccdddd").forEach(System.out::println);
//5.指定长度分割
List<String> list = ("#").limit(3).splitToList("a#b#c#d#e#"); //以#来分割,分3部分成 a b #c#d#e 3部分
System.out.(0));
System.out.(1));
System.out.(2));
⼆.按正则来进⾏分割
字符串截取工具
//1.传⼊字符的分割
//2.传⼊pattern的分割
<(Patternpile("\\|")).omitEmptyStrings().trimResults().splitToList("a|b|c||").forEach(System.out::println);
//3.传⼊pattern 转换成map
  Map<String, String> map = (Patternpile("\\|")).omitEmptyStrings()
                       .trimResults().withKeyValueSeparator("=").split("a=b|c=d"); for (Entry<String, String> entry : Set()) {
  System.out.Key() +" = "+ Value());
}
三.测试
/**
* list转换为字符串
*/
@Test
public void joinTest(){
  List<String> names = wArrayList("John", "Jane", "Adam", "Tom");
  String result = (",").join(names);
  assertEquals(result, "John,Jane,Adam,Tom");
}
/**
* map转换为字符串
*/
@Test
public void whenConvertMapToString_thenConverted() {
  Map<String, Integer> salary = wHashMap();
  salary.put("John", 1000);
  salary.put("Jane", 1500);
  String result = (" , ").withKeyValueSeparator(" = ")
    .join(salary);
  System.out.println(result);
}
/**
* list转String,跳过null
*/
@Test
public void whenConvertListToStringAndSkipNull_thenConverted() {
  List<String> names = wArrayList("John", null, "Jane", "Adam", "Tom");  String result = (",").skipNulls().join(names);
  System.out.println(result);
  assertEquals(result, "John,Jane,Adam,Tom");
}
/**
* list转String,将null变成其他值
*/
@Test
public void whenUseForNull_thenUsed() {
  List<String> names = wArrayList("John", null, "Jane", "Adam", "Tom");  String result = (",").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 = ("-").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 = (",")
      .withKeyValueSeparator("=")
      .split(input);
   assertEquals("first", ("John"));
    assertEquals("second", ("Adam"));
}
/**
* 多个字符进⾏分割
*/
@Test
public void whenSplitStringOnMultipleSeparator_thenSplit() {
  String input = "apple.banana,,orange,,.";
  List<String> result = Pattern("[.|,]")
    .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 = (",")
            .limit(4)
          .splitToList(input);
  assertEquals(4, result.size());  System.out.println(result);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。