StringJoiner详解

一、概述

平时我们进行字符串拼接时,最常用的就是StringBuilder和StringBuffer了,但是在JDK 1.8+中,引入了一个字符串拼接神器:StringJoiner。

 

二、 案例

例如:

hello,world,欢迎使用StringJoiner

在JDK 1.8之前,需要使用StringBuilder或者StringBuffer来进行拼接。

        StringBuilder stb = new StringBuilder();
        stb.append("hello");
        stb.append(",");
        stb.append("world");
        stb.append(",");
        stb.append("欢迎使用StringJoiner");
        System.out.println(stb);

其中包含了许多分隔符,如果拼接的字符串非常多,代码就会显得非常臃肿。但是在JDK 1.8+有了StringJoiner,这件事就变得很简单了,分隔符全部都交给了StringJoiner处理

        StringJoiner stj = new StringJoiner(",");
        stj.add("hello");
        stj.add("world");
        stj.add("欢迎使用StringJoiner");
        System.out.println(stj);

 

三、 StringJoiner的其他用法

1、属性

StringJoiner类结构图

从类结构图中可以看出,StringJoiner的属性有:

prefix:拼接后的字符串前缀
delimiter:拼接时的字符串分隔符
suffix:拼接后的字符串后缀
value:拼接后的值
emptyValue:空值的情况,value为null时返回

2. 公有方法

setEmptyValue():设置空值
toString():转换成String
add():添加字符串
merge():从另一个StringJoiner合并
length():长度(包括前后缀)

3. 构造器

    /**
     * Constructs a {@code StringJoiner} with no characters in it, with no
     * {@code prefix} or {@code suffix}, and a copy of the supplied
     * {@code delimiter}.
     * If no characters are added to the {@code StringJoiner} and methods
     * accessing the value of it are invoked, it will not return a
     * {@code prefix} or {@code suffix} (or properties thereof) in the result,
     * unless {@code setEmptyValue} has first been called.
     *
     * @param  delimiter the sequence of characters to be used between each
     *         element added to the {@code StringJoiner} value
     * @throws NullPointerException if {@code delimiter} is {@code null}
     */
    public StringJoiner(CharSequence delimiter) {
        this(delimiter, "", "");
    }

    /**
     * Constructs a {@code StringJoiner} with no characters in it using copies
     * of the supplied {@code prefix}, {@code delimiter} and {@code suffix}.
     * If no characters are added to the {@code StringJoiner} and methods
     * accessing the string value of it are invoked, it will return the
     * {@code prefix + suffix} (or properties thereof) in the result, unless
     * {@code setEmptyValue} has first been called.
     *
     * @param  delimiter the sequence of characters to be used between each
     *         element added to the {@code StringJoiner}
     * @param  prefix the sequence of characters to be used at the beginning
     * @param  suffix the sequence of characters to be used at the end
     * @throws NullPointerException if {@code prefix}, {@code delimiter}, or
     *         {@code suffix} is {@code null}
     */
    public StringJoiner(CharSequence delimiter,
                        CharSequence prefix,
                        CharSequence suffix) {
        Objects.requireNonNull(prefix, "The prefix must not be null");
        Objects.requireNonNull(delimiter, "The delimiter must not be null");
        Objects.requireNonNull(suffix, "The suffix must not be null");
        // make defensive copies of arguments
        this.prefix = prefix.toString();
        this.delimiter = delimiter.toString();
        this.suffix = suffix.toString();
    }

StringJoiner提供了两个构造器,一个必须带分隔符,另一个必须带分隔符、前缀、后缀。

4. 前后缀拼接

public class Test {
    public static void main(String[] args) {
        StringJoiner stj = new StringJoiner(",", "--", "++");
        stj.add("hello");
        stj.add("world");
        stj.add("欢迎使用StringJoiner");
        System.out.println(stj);
    }
}

--hello,world,欢迎使用StringJoiner++

5. 空值处理

public class Test {
    public static void main(String[] args) {
        StringJoiner stj = new StringJoiner(",");
        System.out.println(stj.toString());
    }
}

输出空白字符串
public class Test {
    public static void main(String[] args) {
        StringJoiner stj = new StringJoiner(",", "[", "]");
        System.out.println(stj.toString());
    }
}

[]
public class Test {
    public static void main(String[] args) {
        StringJoiner stj = new StringJoiner(",", "[", "]");
        stj.setEmptyValue("empty");
        System.out.println(stj.toString());
    }
}

empty

6. String.join()

String.join()是对StringJoiner的又一层封装API,也是出自JDK 1.8+,可以传入动态的参数或者迭代器。

源码:

    /**
     * Returns a new String composed of copies of the
     * {@code CharSequence elements} joined together with a copy of
     * the specified {@code delimiter}.
     *
     * <blockquote>For example,
     * <pre>{@code
     *     String message = String.join("-", "Java", "is", "cool");
     *     // message returned is: "Java-is-cool"
     * }</pre></blockquote>
     *
     * Note that if an element is null, then {@code "null"} is added.
     *
     * @param  delimiter the delimiter that separates each element
     * @param  elements the elements to join together.
     *
     * @return a new {@code String} that is composed of the {@code elements}
     *         separated by the {@code delimiter}
     *
     * @throws NullPointerException If {@code delimiter} or {@code elements}
     *         is {@code null}
     *
     * @see java.util.StringJoiner
     * @since 1.8
     */
    public static String join(CharSequence delimiter, CharSequence... elements) {
        Objects.requireNonNull(delimiter);
        Objects.requireNonNull(elements);
        // Number of elements not likely worth Arrays.stream overhead.
        StringJoiner joiner = new StringJoiner(delimiter);
        for (CharSequence cs: elements) {
            joiner.add(cs);
        }
        return joiner.toString();
    }

    /**
     * Returns a new {@code String} composed of copies of the
     * {@code CharSequence elements} joined together with a copy of the
     * specified {@code delimiter}.
     *
     * <blockquote>For example,
     * <pre>{@code
     *     List<String> strings = List.of("Java", "is", "cool");
     *     String message = String.join(" ", strings);
     *     //message returned is: "Java is cool"
     *
     *     Set<String> strings =
     *         new LinkedHashSet<>(List.of("Java", "is", "very", "cool"));
     *     String message = String.join("-", strings);
     *     //message returned is: "Java-is-very-cool"
     * }</pre></blockquote>
     *
     * Note that if an individual element is {@code null}, then {@code "null"} is added.
     *
     * @param  delimiter a sequence of characters that is used to separate each
     *         of the {@code elements} in the resulting {@code String}
     * @param  elements an {@code Iterable} that will have its {@code elements}
     *         joined together.
     *
     * @return a new {@code String} that is composed from the {@code elements}
     *         argument
     *
     * @throws NullPointerException If {@code delimiter} or {@code elements}
     *         is {@code null}
     *
     * @see    #join(CharSequence,CharSequence...)
     * @see    java.util.StringJoiner
     * @since 1.8
     */
    public static String join(CharSequence delimiter,
            Iterable<? extends CharSequence> elements) {
        Objects.requireNonNull(delimiter);
        Objects.requireNonNull(elements);
        StringJoiner joiner = new StringJoiner(delimiter);
        for (CharSequence cs: elements) {
            joiner.add(cs);
        }
        return joiner.toString();
    }

通过源码可以看出,这两个方法只能进行简单的拼接操作,不能添加前后缀、空值设置处理等。

 

四、参考资料

微信公众号:macrozheng:《你只会StringBuilder?试试StringJoiner,真香!》

  • 23
    点赞
  • 100
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天天向上Charles

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值