Introduction
The StringJoiner
class in Java, part of the java.util
package, is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.
It was introduced in Java 8 as a utility to make it easier to create delimited strings, particularly useful in scenarios where you need to join strings with a specific delimiter.
Table of Contents
- What is the
StringJoiner
Class? - Common Methods
- Examples of Using the
StringJoiner
Class - Conclusion
1. What is the StringJoiner Class?
The StringJoiner
class is designed to concatenate strings with a specified delimiter and optional prefix and suffix. This class provides a convenient way to build a single string from multiple parts with consistent formatting.
2. Common Methods
add(CharSequence newElement)
: Adds a copy of the givenCharSequence
value as the next element of theStringJoiner
value.toString()
: Returns the string representation of theStringJoiner
instance, including the prefix, the delimiter, and the suffix.setEmptyValue(CharSequence emptyValue)
: Sets the sequence of characters to be used when theStringJoiner
is empty.length()
: Returns the length of the string (in characters) constructed so far.
3. Examples of Using the StringJoiner Class
Example 1: Basic Usage of StringJoiner
This example demonstrates basic usage of the StringJoiner
to join strings with a delimiter.
import java.util.StringJoiner;
public class BasicStringJoinerExample {
public static void main(String[] args) {
StringJoiner joiner = new StringJoiner(", ");
joiner.add("Apple");
joiner.add("Banana");
joiner.add("Cherry");
System.out.println(joiner.toString());
}
}
Output:
Apple, Banana, Cherry
Example 2: Using Prefix and Suffix
This example shows how to use StringJoiner
with a prefix and suffix.
import java.util.StringJoiner;
public class PrefixSuffixExample {
public static void main(String[] args) {
StringJoiner joiner = new StringJoiner(", ", "[", "]");
joiner.add("Apple");
joiner.add("Banana");
joiner.add("Cherry");
System.out.println(joiner.toString());
}
}
Output:
[Apple, Banana, Cherry]
Example 3: Using setEmptyValue
This example demonstrates how to set a default value for an empty StringJoiner
.
import java.util.StringJoiner;
public class SetEmptyValueExample {
public static void main(String[] args) {
StringJoiner joiner = new StringJoiner(", ");
joiner.setEmptyValue("No fruits");
System.out.println(joiner.toString()); // Prints "No fruits"
joiner.add("Apple");
joiner.add("Banana");
System.out.println(joiner.toString()); // Prints "Apple, Banana"
}
}
Output:
No fruits
Apple, Banana
Example 4: Merging Two StringJoiner
Instances
This example demonstrates how to merge two StringJoiner
instances.
import java.util.StringJoiner;
public class MergeStringJoinerExample {
public static void main(String[] args) {
StringJoiner joiner1 = new StringJoiner(", ");
joiner1.add("Apple");
joiner1.add("Banana");
StringJoiner joiner2 = new StringJoiner("; ");
joiner2.add("Cherry");
joiner2.add("Date");
joiner1.merge(joiner2);
System.out.println(joiner1.toString());
}
}
Output:
Apple, Banana, Cherry; Date
Example 5: Getting the Length of the Joined String
This example demonstrates how to get the length of the string constructed by StringJoiner
.
import java.util.StringJoiner;
public class StringJoinerLengthExample {
public static void main(String[] args) {
StringJoiner joiner = new StringJoiner(", ");
joiner.add("Apple");
joiner.add("Banana");
joiner.add("Cherry");
System.out.println("Joined String: " + joiner.toString());
System.out.println("Length: " + joiner.length());
}
}
Output:
Joined String: Apple, Banana, Cherry
Length: 21
4. Conclusion
The StringJoiner
class in Java provides a convenient way to construct delimited strings with optional prefix and suffix. It simplifies the process of joining multiple strings into a single formatted string. The examples provided demonstrate common usage patterns and highlight the capabilities of the StringJoiner
class, making it used for string manipulation tasks in Java.
Comments
Post a Comment
Leave Comment