📜  Java中的StringJoiner类

📅  最后修改于: 2022-05-13 01:55:45.233000             🧑  作者: Mango

Java中的StringJoiner类

StringJoiner 是Java.util 包中的一个类,用于构造由分隔符分隔的字符序列(字符串),并且可以选择以提供的前缀开头并以给定的后缀结尾。尽管这也可以借助 StringBuilder 类在每个字符串后附加分隔符来完成,但 StringJoiner 提供了一种简单的方法来做到这一点,而无需编写太多代码。

StringJoiner 类的构造函数

1. StringJoiner(CharSequence delimiter):它构造一个没有字符、没有前缀或后缀的StringJoiner,以及提供的分隔符的副本。

句法:

public StringJoiner(CharSequence delimiter)

参数:添加到 StringJoiner 值的每个元素之间要使用的字符序列

抛出异常:如果分隔符为 null,则为 NullPointerException

2. StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix):它使用提供的前缀、分隔符和后缀的副本构造一个没有字符的StringJoiner。如果没有字符被添加到 StringJoiner 并且调用了访问字符串值的方法,它将在结果中返回前缀 + 后缀(或其属性),除非首先调用了setEmptyValue

句法:

public StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)

参数:

  • 添加到 StringJoiner 值的每个元素之间要使用的字符序列
  • 开头使用的字符序列
  • 最后要使用的字符序列

抛出异常:如果前缀、分隔符或后缀为空,则为 NullPointerException。

StringJoiner 类的方法

Method Action Performed
add()Adds a copy of the given CharSequence value as the next element of the StringJoiner value. If newElement is null, then “null” is added. 
length() Returns the length of the String representation of this StringJoiner. 
merge()Adds the contents of the given StringJoiner without prefix and suffix as the next element if it is non-empty. If the given StringJoiner is empty, the call has no effect. Suppose the other StringJoiner is using a different delimiter. In that case, elements from the other StringJoiner are concatenated with that delimiter, and the result is appended to this StringJoiner as a single element. 
toString()Returns the String object of this StringJoiner
setEmptyValue()Sets the string to be used when determining the string representation of this StringJoiner, and no elements have been added yet; that is when it is empty

例子:

Java
// Java program to Demonstrate Methods
// of StringJoiner class
 
// Importing required classes
import java.util.ArrayList;
import java.util.StringJoiner;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an empty ArrayList of string type
        ArrayList al = new ArrayList<>();
 
        // Adding elements to above List
        al.add("Ram");
        al.add("Shyam");
        al.add("Alice");
        al.add("Bob");
 
        // Creating object of class inside main()
        StringJoiner sj1 = new StringJoiner(",");
 
        // Using setEmptyValue() method
        sj1.setEmptyValue("sj1 is empty");
        System.out.println(sj1);
 
        // Using add() method
        sj1.add(al.get(0)).add(al.get(1));
        System.out.println(sj1);
 
        // Using length() method
        System.out.println("Length of sj1 : "
                           + sj1.length());
 
        StringJoiner sj2 = new StringJoiner(":");
        sj2.add(al.get(2)).add(al.get(3));
 
        // Using merge() method
        sj1.merge(sj2);
 
        // Using toString() method
        System.out.println(sj1.toString());
 
        System.out.println("Length of new sj1 : "
                           + sj1.length());
    }
}


输出
sj1 is empty
Ram,Shyam
Length of sj1 : 9
Ram,Shyam,Alice:Bob
Length of new sj1 : 19