Guava之Splitter

Splitter相当于拆分器,跟Joiner正好是相反的操作



生成 Splitter 的方法

工厂方法:

  • Splitter on(char separator)
  • Splitter on(final CharMatcher separatorMatcher)
  • Splitter on(final String separator)
  • Splitter on(final Pattern separatorPattern): 支持正则
  • Splitter onPattern(String separatorPattern): 等效于Splitter.on(Pattern.compile(pattern))
  • Splitter fixedLength(final int length): 每length个元素分成一组

修饰方法:

  • Splitter omitEmptyStrings()
  • Splitter limit(int limit)
  • Splitter trimResults()
  • Splitter trimResults(CharMatcher trimmer)

示例:

Splitter.on(',').split("foo,,bar");  // ["foo", "", "bar"]
Splitter.on(CharMatcher.anyOf(";,")).split("foo,;bar,quux"); // ["foo", "", "bar", "quux"]
Splitter.on(", ").split("foo, bar,baz"); // ["foo", "bar,baz"]
Splitter.on(Pattern.compile("\\d+")).split("a12b3c"); // [a, b, c]
Splitter.fixedLength(2).split("abcde"); // ["ab", "cd", "e"]
Splitter.on(',').limit(3).split("a,b,c,d"); // ["a", "b", "c,d"]
Splitter.on(',').limit(3).omitEmptyStrings().split("a,,,b,,,c,d"); // ["a", "b", "c,d"]
Splitter.on(',').limit(3).trimResults().split(" a , b , c , d "); // ["a", "b", "c , d"]
Splitter.on(',').trimResults(CharMatcher.is('_')).split("_a ,_b_ ,c__"); // ["a ", "b_ ", "c"]

split 方法

  • Iterable<String> split(final CharSequence sequence)
  • List<String> splitToList(CharSequence sequence)

MapSplitter

Splitter提供了个方法返回内部类Splitter.MapSplitter专门分割成map

  • MapSplitter withKeyValueSeparator(String separator)
  • MapSplitter withKeyValueSeparator(char separator)
  • MapSplitter withKeyValueSeparator(Splitter keyValueSplitter)

注意: MapSplittersplit方法返回的是Map<String, String>

Map<String, String> map = Splitter.on("&").withKeyValueSeparator("=").split("a=1&b=2&c=3");