🎓 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, 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
StringJoinerClass? - Common Methods
- Examples of Using the
StringJoinerClass - 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 givenCharSequencevalue as the next element of theStringJoinervalue.toString(): Returns the string representation of theStringJoinerinstance, including the prefix, the delimiter, and the suffix.setEmptyValue(CharSequence emptyValue): Sets the sequence of characters to be used when theStringJoineris 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.
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