🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
StringJoinerInstances - 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
StringJoineris created with a comma and a space (,) as the delimiter. - Strings are added to the
StringJoinerusing theadd()method. - The
toString()method is used to convert theStringJoinerto 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
StringJoineris 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
StringJoinerinstances are created and populated with strings. - The
merge()method merges the secondStringJoinerinto the first one. - The resulting string includes all the elements from both
StringJoinerinstances, 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!
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment