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:
Java
// 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());
}
}
OutputJoined 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.
Java
// 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());
}
}
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:
Java
// 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());
}
}
OutputStringJoiner with elements: geeks, for, geeks
Empty StringJoiner: No elements to join
StringJoiner with prefix and suffix: [geeks | for | geeks]
Length of joined string: 17
Similar Reads
Java StringBuilder Class In Java, the StringBuilder class is a part of the java.lang package that provides a mutable sequence of characters. Unlike String (which is immutable), StringBuilder allows in-place modifications, making it memory-efficient and faster for frequent string operations.Declaration:StringBuilder sb = new
7 min read
Java Writer Class Java writer class is an abstract class in the java.io package. It is designed for writing character streams. Writer class in Java provides methods for writing characters, arrays of characters, and strings. Since it is an abstract class, we cannot create an instance of it directly. Instead, we will u
5 min read
StringJoiner add() method in Java The add(CharSequence newElement) of StringJoiner adds a copy of the given CharSequence value as the next element of the StringJoiner value. If newElement is null, then "null" is added.Syntax: public StringJoiner add(CharSequence newElement) Parameters: This method takes a mandatory parameter newElem
1 min read
Java Pattern Class The Pattern class in Java is used for defining regular expressions (regex) to perform pattern matching on strings. It is part of the java.util.regex package and it plays a key role in searching, replacing, and manipulating strings based on patterns. The Matcher class works together with Pattern to p
3 min read
Matcher Class in Java In Java, Matcher is a class that is implemented by the MatchResult interface, that performs match operations on a character sequence by interpreting a Pattern. Below, we can see the declaration of java.util.regex.Matcher in java.lang.Object Class: public final class Matcher extends Object implements
4 min read