📜  Java8 StringJoiner

📅  最后修改于: 2020-10-13 03:32:40             🧑  作者: Mango

Java StringJoiner

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

StringJoiner构造函数

Constructor Description
Public StringJoiner(CharSequence delimiter) It constructs a StringJoiner with no characters in it, with no prefix or suffix, and a copy of the supplied delimiter.
It throws NullPointerException if delimiter is null.
Public StringJoiner(CharSequence delimiter,CharSequence prefix,CharSequence suffix) It constructs a StringJoiner with no characters in it using copies of the supplied prefix, delimiter and suffix.
It throws NullPointerException if prefix, delimiter, or suffix is null.

StringJoiner方法

Method Description
Public StringJoiner add(CharSequence newElement) It adds a copy of the given CharSequence value as the next element of the StringJoiner value. If newElement is null,”null” is added.
Public StringJoiner merge(StringJoiner other) It 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.
Public int length() It returns the length of the String representation of this StringJoiner.
Public StringJoiner setEmptyValue(CharSequence emptyValue) It sets the sequence of characters 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 StringJoiner示例

// 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

Java StringJoiner示例:添加前缀和后缀

// 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]

StringJoiner示例:合并两个StringJoiner

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

// 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]

StringJoiner示例:StringJoiner方法

// 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