【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.   
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值