Java StringJoiner is a class introduced in Java 8 that provides a way to join strings with a delimiter and optional prefix and suffix. It is a convenient and efficient alternative to using a StringBuilder or StringBuffer to manually append strings with delimiters.
The StringJoiner class provides three constructors:
StringJoiner(CharSequence delimiter)
: Creates a new StringJoiner with the specified delimiter.StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
: Creates a new StringJoiner with the specified delimiter, prefix, and suffix.StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
: Creates a new StringJoiner with the specified delimiter, prefix, and suffix, and with the ability to handle empty values.
To use StringJoiner, you first create an instance with the desired delimiter and optional prefix and suffix. You can then add strings to the joiner using the add
method, which appends the string to the current string being built and adds the delimiter if necessary. Finally, you can retrieve the joined string using the toString
method.
Here’s an example:
StringJoiner joiner = new StringJoiner(", ", "[", "]"); joiner.add("apple"); joiner.add("banana"); joiner.add("orange"); String joined = joiner.toString(); // "[apple, banana, orange]"
In this example, the delimiter is a comma followed by a space, the prefix is an opening square bracket, and the suffix is a closing square bracket. The add
method is called three times to add the strings “apple”, “banana”, and “orange”, respectively. Finally, the toString
method is called to retrieve the joined string.
StringJoiner Constructors:
Sure! The StringJoiner
class in Java provides three constructors that you can use to create instances of the class. Here are the details of each constructor:
StringJoiner(CharSequence delimiter)
: This constructor creates a newStringJoiner
instance with the specified delimiter. Thedelimiter
parameter is a character sequence that is used to separate the strings that will be added to the joiner. For example, if you specify a delimiter of “, “, the joiner will separate the strings with a comma followed by a space.StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
: This constructor creates a newStringJoiner
instance with the specified delimiter, prefix, and suffix. Thedelimiter
parameter is the same as in the previous constructor. Theprefix
parameter is a character sequence that will be added to the beginning of the joined string. Thesuffix
parameter is a character sequence that will be added to the end of the joined string. For example, if you specify a delimiter of “, “, a prefix of “[“, and a suffix of “]”, the joiner will produce a string that starts with “[” and ends with “]” and separates the strings with a comma followed by a space.StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
: This constructor creates a newStringJoiner
instance with the specified delimiter, prefix, suffix, and handling for empty values. Thedelimiter
,prefix
, andsuffix
parameters are the same as in the previous constructor. The additional parameter,emptyValue
, is a character sequence that will be used instead of an empty string if an empty value is added to the joiner. If you don’t specify anemptyValue
, an empty string will be used by default. For example, if you specify a delimiter of “, “, a prefix of “[“, a suffix of “]”, and an empty value of “(none)”, the joiner will produce a string that starts with “[” and ends with “]” and separates the strings with a comma followed by a space. If an empty value is added to the joiner, it will be replaced with “(none)” instead of an empty string.
StringJoiner Methods:
Sure, here are the methods provided by the StringJoiner
class in Java:
add(CharSequence csq)
: This method adds the specified character sequence to the joiner. If this is the first string added, it is added without a delimiter. If there are already one or more strings in the joiner, the delimiter is added before the new string.merge(StringJoiner other)
: This method merges the contents of the specifiedStringJoiner
other
into thisStringJoiner
. The delimiter of the otherStringJoiner
is used between the last element of thisStringJoiner
and the first element of the otherStringJoiner
.toString()
: This method returns the joined string as aString
object.length()
: This method returns the length of the current string being built by theStringJoiner
. The length includes the prefix and suffix, but not the delimiter.setEmptyValue(CharSequence emptyValue)
: This method sets the value that will be used instead of an empty string when the joiner is empty. By default, an empty string is used.
These are the main methods provided by the StringJoiner
class. You can use them to add strings, retrieve the joined string, and modify the joiner’s behavior.
Java StringJoiner Example:
Sure! Here’s an example that demonstrates how to use the StringJoiner
class in Java:
import java.util.StringJoiner; public class StringJoinerExample { public static void main(String[] args) { StringJoiner joiner = new StringJoiner(", ", "[", "]"); joiner.add("apple"); joiner.add("banana"); joiner.add("orange"); String fruits = joiner.toString(); System.out.println(fruits); // [apple, banana, orange] } }
In this example, we create a new StringJoiner
instance with a delimiter of “, “, a prefix of “[“, and a suffix of “]”. We then add three strings (“apple”, “banana”, and “orange”) to the joiner using the add()
method. Finally, we retrieve the joined string using the toString()
method and print it to the console.
When you run this example, the output will be:
[apple, banana, orange]
As you can see, the StringJoiner
class makes it easy to join strings with a delimiter and optional prefix and suffix.
Java StringJoiner Example: adding prefix and suffix:
Sure, here’s an example that demonstrates how to use the StringJoiner
class with a prefix and a suffix:
import java.util.StringJoiner; public class StringJoinerExample { public static void main(String[] args) { StringJoiner joiner = new StringJoiner(", ", "[", "]"); joiner.add("apple"); joiner.add("banana"); joiner.add("orange"); String fruits = joiner.toString(); System.out.println(fruits); // [apple, banana, orange] StringJoiner joiner2 = new StringJoiner(", ", "Fruits: [", "]"); joiner2.add("apple"); joiner2.add("banana"); joiner2.add("orange"); String fruits2 = joiner2.toString(); System.out.println(fruits2); // Fruits: [apple, banana, orange] } }
In this example, we create two instances of StringJoiner
with different prefixes and suffixes. The first StringJoiner
instance has a prefix of “[” and a suffix of “]”, while the second StringJoiner
instance has a prefix of “Fruits: [” and a suffix of “]”. We then add three strings (“apple”, “banana”, and “orange”) to each joiner using the add()
method. Finally, we retrieve the joined string using the toString()
method and print it to the console.
When you run this example, the output will be:
[apple, banana, orange] Fruits: [apple, banana, orange]
As you can see, the StringJoiner
class allows you to specify a prefix and a suffix that are added to the beginning and end of the joined string, respectively.
StringJoiner Example: Merge Two StringJoiner
Sure, here’s an example that demonstrates how to merge two instances of StringJoiner
using the merge()
method:
import java.util.StringJoiner; public class StringJoinerExample { public static void main(String[] args) { StringJoiner joiner1 = new StringJoiner(", "); joiner1.add("apple"); joiner1.add("banana"); joiner1.add("orange"); StringJoiner joiner2 = new StringJoiner(", "); joiner2.add("grape"); joiner2.add("pineapple"); joiner1.merge(joiner2); String fruits = joiner1.toString(); System.out.println(fruits); // apple, banana, orange, grape, pineapple } }
In this example, we create two instances of StringJoiner
, joiner1
and joiner2
. We add three strings to joiner1
(“apple”, “banana”, and “orange”) and two strings to joiner2
(“grape” and “pineapple”). We then merge joiner2
into joiner1
using the merge()
method. Finally, we retrieve the joined string from joiner1
using the toString()
method and print it to the console.
When you run this example, the output will be:
apple, banana, orange, grape, pineapple
As you can see, the merge()
method allows you to combine two instances of StringJoiner
into one. The delimiter of the first StringJoiner
is used between the last element of the first joiner and the first element of the second joiner. In this example, the delimiter is “, “.
StringJoiner Example: StringJoiner Methods
Sure, here’s an example that demonstrates how to use some of the methods of the StringJoiner
class:
import java.util.StringJoiner; public class StringJoinerExample { public static void main(String[] args) { StringJoiner joiner = new StringJoiner(", "); joiner.setEmptyValue("No fruits yet"); System.out.println(joiner.toString()); // No fruits yet joiner.add("apple"); joiner.add("banana"); joiner.add("orange"); System.out.println(joiner.toString()); // apple, banana, orange joiner.add(""); joiner.add("grape"); joiner.add("pineapple"); System.out.println(joiner.toString()); // apple, banana, orange, , grape, pineapple joiner.setEmptyValue("No fruits at all"); System.out.println(joiner.toString()); // apple, banana, orange, , grape, pineapple joiner = new StringJoiner(":"); joiner.add("1"); joiner.add("2"); joiner.add("3"); System.out.println(joiner.toString()); // 1:2:3 } }
In this example, we create an instance of StringJoiner
with a delimiter of “, “. We set the empty value of the joiner to “No fruits yet” using the setEmptyValue()
method and retrieve the joined string using the toString()
method. Since the joiner is empty, the output is “No fruits yet”.
We then add three strings (“apple”, “banana”, and “orange”) to the joiner using the add()
method and retrieve the joined string using the toString()
method. The output is “apple, banana, orange”.
We then add an empty string and two more strings (“grape” and “pineapple”) to the joiner using the add()
method and retrieve the joined string using the toString()
method. The output is “apple, banana, orange, , grape, pineapple”.
We then set the empty value of the joiner to “No fruits at all” using the setEmptyValue()
method and retrieve the joined string using the toString()
method. Since the joiner is not empty, the output is still “apple, banana, orange, , grape, pineapple”.
Finally, we create a new instance of StringJoiner
with a delimiter of “:” and add three strings (“1”, “2”, and “3”) to the joiner using the add()
method. We retrieve the joined string using the toString()
method and the output is “1:2:3”.
As you can see, the StringJoiner
class provides several methods for manipulating the joiner, including setting the empty value, adding elements, and changing the delimiter.