【Guava】Joiner、Splitter源码

Joiner

作用:将多个对象拼接成一个字符串
  • 示例

        Joiner joiner = Joiner.on(";").skipNulls();
        return joiner.join(bpeApplication, pipeline, deviceName, field);
  • 源码

  /**
   * Returns a joiner which automatically places {@code separator} between consecutive elements.
   */
  public static Joiner on(String separator) {
    return new Joiner(separator);
  }
  /**
   * Returns a string containing the string representation of each argument, using the previously
   * configured separator between each.
   */
  public final String join(@Nullable Object first, @Nullable Object second, Object... rest) {
    return join(iterable(first, second, rest));
  }
  public final String join(Iterable<?> parts) {
    return join(parts.iterator());
  }
  public final String join(Iterator<?> parts) {
    return appendTo(new StringBuilder(), parts).toString();
  }
  public final String join(Iterator<?> parts) {
    return appendTo(new StringBuilder(), parts).toString();
  }
 
  public <A extends Appendable> A appendTo(A appendable, Iterator<?> parts) throws IOException {
    checkNotNull(appendable);
    if (parts.hasNext()) {
      appendable.append(toString(parts.next()));
      while (parts.hasNext()) {
        appendable.append(separator);
        appendable.append(toString(parts.next()));
      }
    }
    return appendable;
  }

Splitter

Splitter提供Joiner对应的功能。
  • 示例

Splitter.on(',').split("foo,bar")
 
返回:This invocation returns an Iterable<String> containing "foo" and "bar", in that order.

 

By default Splitter's behavior is very simplistic:

Splitter.on(',').split("foo,,bar, quux")
 
This returns an iterable containing ["foo", "", "bar", " quux"]. Notice that the splitter does not assume that you want empty strings removed, or that you wish to trim whitespace. If you want features like these, simply ask for them:

private static final Splitter MY_SPLITTER = Splitter.on(',')
       .trimResults()
       .omitEmptyStrings();
 
Now MY_SPLITTER.split("foo, ,bar, quux,") returns an iterable containing just ["foo", "bar", "quux"]. Note that the order in which the configuration methods are called is never significant; for instance, trimming is always applied first before checking for an empty result, regardless of the order in which the trimResults() and omitEmptyStrings() methods were invoked.  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读