Java StringJoiner Class
StringJoiner class in Java provides an efficient way to concatenate multiple strings with a defined delimiter(character), optional prefix, and suffix. This class is especially useful when constructing formatted strings dynamically.
Example 1:
// Demonstration of StringJoiner Class
import java.util.StringJoiner;
public class Geeks{
public static void main(String[] args) {
// Create a StringJoiner with a delimiter
StringJoiner sj = new StringJoiner(", ");
// Add strings which you need to join
sj.add("geeks").add("for").add("geeks");
System.out.println("Joined String: " + sj.toString());
}
}
Output
Joined String: geeks, for, geeks
Explanation: In the above example, we have created a StringJoiner
object with a comma and space as the delimiter. The toString()
method is used to make the addition of string with the specified delimiter.
Example 2: Here, we will use the StringJoiner
to join elements of an array of Strings.
// Using StringJoiner with Array of Strings
import java.util.StringJoiner;
public class Geeks {
public static void main(String[] args) {
String[] s1 = {"Geeks", "for", "Geeks"};
// Create StringJoiner with a delimiter
StringJoiner sj = new StringJoiner(", ");
// Adding array elements to StringJoiner
for (String s2 : s1) {
sj.add(s2);
}
System.out.println(sj.toString());
}
}
Output
Geeks, for, Geeks
Key Features:
- Delimiters: Define a separator between the strings.
- Prefix and Suffix: Add optional characters before and after the final joined string.
- Chaining: Easily chain calls to add elements for a clean and concise implementation.
Constructors of StringJoiner Class
1. StringJoiner(CharSequence delimiter): It creates a StringJoiner with a specified delimiter.
Syntax:
public StringJoiner(CharSequence delimiter)
2. StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix): It creates a StringJoiner with a delimiter, prefix, and suffix.
Syntax:
public StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
Methods of StringJoiner Class
Method | Action Performed |
---|---|
add() | Adds a string to the StringJoiner. |
length() | Returns the length of the joined string. |
merge() | Merges the contents of another StringJoiner into the current one. |
toString() | Returns the final joined string. |
setEmptyValue() | Specifies a default value if no elements are added. |
Example:
// Demostrating different methods
// of StringJoiner Class
import java.util.StringJoiner;
public class Geeks {
public static void main(String[] args) {
// add() method
StringJoiner sj1 = new StringJoiner(", ");
sj1.add("geeks").add("for").add("geeks");
System.out.println("StringJoiner with elements: " + sj1.toString());
// setEmptyValue() method
StringJoiner sj2 = new StringJoiner(", ");
sj2.setEmptyValue("No elements to join");
System.out.println("Empty StringJoiner: " + sj2.toString());
// toString() method
StringJoiner sj3 = new StringJoiner(" | ", "[", "]");
sj3.add("geeks").add("for").add("geeks");
System.out.println("StringJoiner with prefix and suffix: " + sj3.toString());
// Checking the length of the joined string
StringJoiner sj4 = new StringJoiner(", ");
sj4.add("geeks").add("for").add("geeks");
System.out.println("Length of joined string: " + sj4.length());
}
}
Output
StringJoiner with elements: geeks, for, geeks Empty StringJoiner: No elements to join StringJoiner with prefix and suffix: [geeks | for | geeks] Length of joined string: 17