Introduction
The StringJoiner
class in Java is part of the java.util
package and was introduced in Java 8. It constructs a sequence of characters separated by a delimiter, optionally starting with a supplied prefix and ending with a supplied suffix. This class can be particularly useful when concatenating multiple strings with a specific delimiter.
Table of Contents
- Basic Usage of
StringJoiner
- Using Prefix and Suffix
- Merging
StringJoiner
Instances - Complete Example Program
- Conclusion
1. Basic Usage of StringJoiner
The simplest usage of StringJoiner
involves specifying a delimiter to separate the strings.
Example:
import java.util.StringJoiner;
public class StringJoinerBasicExample {
public static void main(String[] args) {
// Create a StringJoiner with a delimiter
StringJoiner joiner = new StringJoiner(", ");
// Add strings to the StringJoiner
joiner.add("Apple");
joiner.add("Banana");
joiner.add("Cherry");
// Convert the StringJoiner to a String
String result = joiner.toString();
System.out.println("Joined String: " + result);
}
}
Output:
Joined String: Apple, Banana, Cherry
Explanation:
- A
StringJoiner
is created with a comma and a space (,
) as the delimiter. - Strings are added to the
StringJoiner
using theadd()
method. - The
toString()
method is used to convert theStringJoiner
to aString
.
2. Using Prefix and Suffix
You can also specify a prefix and suffix to be added to the resulting string.
Example:
import java.util.StringJoiner;
public class StringJoinerWithPrefixSuffix {
public static void main(String[] args) {
// Create a StringJoiner with a delimiter, prefix, and suffix
StringJoiner joiner = new StringJoiner(", ", "[", "]");
// Add strings to the StringJoiner
joiner.add("Apple");
joiner.add("Banana");
joiner.add("Cherry");
// Convert the StringJoiner to a String
String result = joiner.toString();
System.out.println("Joined String with Prefix and Suffix: " + result);
}
}
Output:
Joined String with Prefix and Suffix: [Apple, Banana, Cherry]
Explanation:
- A
StringJoiner
is created with a comma and a space as the delimiter,[
as the prefix, and]
as the suffix. - The resulting string includes the prefix and suffix.
3. Merging StringJoiner Instances
You can merge two StringJoiner
instances using the merge()
method.
Example:
import java.util.StringJoiner;
public class StringJoinerMergeExample {
public static void main(String[] args) {
// Create the first StringJoiner
StringJoiner joiner1 = new StringJoiner(", ", "{", "}");
joiner1.add("Apple");
joiner1.add("Banana");
// Create the second StringJoiner
StringJoiner joiner2 = new StringJoiner(", ");
joiner2.add("Cherry");
joiner2.add("Date");
// Merge the second StringJoiner into the first one
joiner1.merge(joiner2);
// Convert the merged StringJoiner to a String
String result = joiner1.toString();
System.out.println("Merged StringJoiner: " + result);
}
}
Output:
Merged StringJoiner: {Apple, Banana, Cherry, Date}
Explanation:
- Two
StringJoiner
instances are created and populated with strings. - The
merge()
method merges the secondStringJoiner
into the first one. - The resulting string includes all the elements from both
StringJoiner
instances, separated by the delimiter of the firstStringJoiner
.
4. Complete Example Program
Here is a complete program that demonstrates the different methods of using StringJoiner
.
Example Code:
import java.util.StringJoiner;
public class StringJoinerExample {
public static void main(String[] args) {
// Basic Usage
StringJoiner joinerBasic = new StringJoiner(", ");
joinerBasic.add("Apple");
joinerBasic.add("Banana");
joinerBasic.add("Cherry");
System.out.println("Basic Usage: " + joinerBasic.toString());
// Usage with Prefix and Suffix
StringJoiner joinerWithPrefixSuffix = new StringJoiner(", ", "[", "]");
joinerWithPrefixSuffix.add("Apple");
joinerWithPrefixSuffix.add("Banana");
joinerWithPrefixSuffix.add("Cherry");
System.out.println("With Prefix and Suffix: " + joinerWithPrefixSuffix.toString());
// Merging StringJoiner Instances
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("Merged StringJoiner: " + joiner1.toString());
}
}
Output:
Basic Usage: Apple, Banana, Cherry
With Prefix and Suffix: [Apple, Banana, Cherry]
Merged StringJoiner: {Apple, Banana, Cherry, Date}
5. Conclusion
The StringJoiner
class in Java provides a flexible and efficient way to concatenate multiple strings with a specified delimiter, prefix, and suffix. It is particularly useful for creating formatted output and generating CSV-like strings. By understanding and using the different methods of StringJoiner
, you can simplify and streamline your string concatenation tasks.
Happy coding!
Comments
Post a Comment
Leave Comment