Java8 StringJoiner类

1 Java8 StringJoiner类的介绍

Java在java.util包中添加了一个新的最终类StringJoiner。它用于构造由定界符分隔的字符序列。现在,您可以通过传递诸如comma(,),hyphen(-)等分隔符来创建字符串。还可以将前缀和后缀传递给char序列。

2 Java8 StringJoiner类的构造方法

构造方法描述
public StringJoiner(CharSequence delimiter)构造一个不带任何字符,没有前缀或后缀的StringJoiner,以及提供的定界符的副本。如果分隔符为null,则抛出NullPointerException。
public StringJoiner(CharSequence delimiter,CharSequence prefix,CharSequence suffix)使用提供的前缀,定界符和后缀的副本构造一个不带任何字符的StringJoiner。如果前缀,分隔符或后缀为null,则抛出NullPointerException。

3 Java8 StringJoiner类的方法

方法描述
public StringJoiner add(CharSequence newElement)将给定CharSequence值的副本添加为StringJoiner值的下一个元素。如果newElement为null,则添加“ null”。
public StringJoiner merge(StringJoiner other)如果它不是空的,它将添加不带前缀和后缀的给定StringJoiner的内容作为下一个元素。如果给定的StringJoiner为空,则该调用无效。
public int length()返回此StringJoiner的String表示形式的长度。
public StringJoiner setEmptyValue(CharSequence emptyValue)设置确定此StringJoiner的字符串表示形式时要使用的字符序列,并且尚未添加任何元素(即,当它为空时)。

4 Java8 StringJoiner类案例

/**
 * https://www.jhelp.com
 */
// importing StringJoiner class  
import java.util.StringJoiner;  
public class StringJoinerExample {  
    public static void main(String[] args) {  
        StringJoiner joinNames = new StringJoiner(","); // passing comma(,) as delimiter   
          
        // Adding values to StringJoiner  
        joinNames.add("Rahul");  
        joinNames.add("Raju");  
        joinNames.add("Peter");  
        joinNames.add("Raheem");  
                  
        System.out.println(joinNames);  
    }  
}  

输出结果为:

Rahul,Raju,Peter,Raheem

5 Java8 StringJoiner类案例:添加前缀和后缀

/**
 * https://www.jhelp.com
 */
// importing StringJoiner class  
import java.util.StringJoiner;  
public class StringJoinerExample {  
    public static void main(String[] args) {  
        StringJoiner joinNames = new StringJoiner(",", "[", "]");   // passing comma(,) and square-brackets as delimiter   
          
        // Adding values to StringJoiner  
        joinNames.add("Rahul");  
        joinNames.add("Raju");  
        joinNames.add("Peter");  
        joinNames.add("Raheem");  
                  
        System.out.println(joinNames);  
    }  
}  

输出结果为:

[Rahul,Raju,Peter,Raheem]

6 Java8 StringJoiner类案例:合并两个StringJoiner

merge() 方法合并两个StringJoiner对象,但不包括第二个StringJoiner对象的前缀和后缀。

/**
 * https://www.jhelp.com
 */
// importing StringJoiner class  
import java.util.StringJoiner;  
public class StringJoinerExample {  
    public static void main(String[] args) {  
  
        StringJoiner joinNames = new StringJoiner(",", "[", "]");   // passing comma(,) and square-brackets as delimiter   
          
        // Adding values to StringJoiner  
        joinNames.add("Rahul");  
        joinNames.add("Raju");  
  
        // Creating StringJoiner with :(colon) delimiter  
        StringJoiner joinNames2 = new StringJoiner(":", "[", "]");  // passing colon(:) and square-brackets as delimiter   
          
        // Adding values to StringJoiner  
        joinNames2.add("Peter");  
        joinNames2.add("Raheem");  
  
        // Merging two StringJoiner  
        StringJoiner merge = joinNames.merge(joinNames2);   
        System.out.println(merge);  
    }  
}  

输出结果为:

[Rahul,Raju,Peter:Raheem]

7 Java8 StringJoiner类案例:StringJoiner方法

/**
 * https://www.jhelp.com
 */
// importing StringJoiner class  
import java.util.StringJoiner;  
public class StringJoinerExample {  
    public static void main(String[] args) {  
        StringJoiner joinNames = new StringJoiner(","); // passing comma(,) as delimiter   
          
        // Prints nothing because it is empty  
        System.out.println(joinNames);  
          
        // We can set default empty value.  
        joinNames.setEmptyValue("It is empty");  
        System.out.println(joinNames);  
          
          
        // Adding values to StringJoiner  
        joinNames.add("Rahul");  
        joinNames.add("Raju");  
        System.out.println(joinNames);  
          
        // Returns length of StringJoiner  
        int length = joinNames.length();  
        System.out.println("Length: "+length);  
          
        // Returns StringJoiner as String type   
        String str = joinNames.toString();  
        System.out.println(str);  
          
        // Now, we can apply String methods on it  
        char ch = str.charAt(3);  
        System.out.println("Character at index 3: "+ch);  
          
        // Adding one more element   
        joinNames.add("Sorabh");  
        System.out.println(joinNames);  
          
        // Returns length  
        int newLength = joinNames.length();  
        System.out.println("New Length: "+newLength);  
    }  
} 

输出结果为:

It is empty
Rahul,Raju
Length: 10
Rahul,Raju
Character at index 3: u
Rahul,Raju,Sorabh
New Length: 17
腾讯云推出云产品限时特惠抢购活动:2C2G云服务器7.9元/月起
本文链接:https://www.jhelp.net/p/Te1nqruIkl5gIKCP (转载请保留)。
关注下面的标签,发现更多相似文章