Java added a new final class StringJoiner in java.util package. It is used to construct a sequence of characters separated by a delimiter. Now, we can create string by passing delimiters like comma(,), hyphen(-) etc. We can also pass prefix and suffix to the char sequence. It is especially useful when constructing formatted strings dynamically.
| 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. |
| 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
import java.util.StringJoiner;
public class Main {
public static void main(String[] args) {
StringJoiner joinNames = new StringJoiner(", ");// passing comma(,) as delimiter
// Adding values to StringJoiner
joinNames.add("James");
joinNames.add("Oliver");
joinNames.add("Peter");
joinNames.add("Robert");
System.out.println(joinNames);
}
}
Output:
James, Oliver, Peter, Robert
Java
import java.util.StringJoiner;
public class Main {
public static void main(String[] args) {
StringJoiner joinNames = new StringJoiner(", ", "[", "]");// passing comma(,) and square-brackets as delimiter
// Adding values to StringJoiner
joinNames.add("James");
joinNames.add("Oliver");
joinNames.add("Peter");
joinNames.add("Robert");
System.out.println(joinNames);
}
}
Output:
[James, Oliver, Peter, Robert]
The merge() method merges two StringJoiner objects excluding of prefix and suffix of second StringJoiner object.
Java
import java.util.StringJoiner;
public class Main {
public static void main(String[] args) {
StringJoiner joinNames = new StringJoiner(", ", "[", "]"); // passing comma(,) and square-brackets as delimiter
// Adding values to StringJoiner
joinNames.add("Robert");
joinNames.add("Jack");
// 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("Olive");
// Merging two StringJoiner
StringJoiner merge = joinNames.merge(joinNames2);
System.out.println(merge);
}
}
Output:
[Robert, Jack, Peter: Olive]
Java
import java.util.StringJoiner;
public class Main {
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("Jack");
joinNames.add("Robert");
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("Tom");
System.out.println(joinNames);
// Returns length
int newLength = joinNames.length();
System.out.println("New Length: "+newLength);
}
}
Output:
It is empty Jack, Robert Length: 12 Jack, Robert Character at index 3: k Jack, Robert, Tom New Length: 17
We request you to subscribe our newsletter for upcoming updates.