🎓 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
String.join() method in Java is used to join multiple strings with a specified delimiter. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
joinMethod Syntax- Examples
- Joining Strings with a Delimiter
- Joining Strings with Different Delimiters
- Joining an Array of Strings
- Joining a List of Strings
- Conclusion
Introduction
The String.join() method is a static method in the String class introduced in Java 8. It allows you to concatenate multiple strings using a specified delimiter, making it easy to create delimited strings.
join Method Syntax
The syntax for the join method is as follows:
public static String join(CharSequence delimiter, CharSequence... elements)
And another variation that accepts an Iterable:
public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
Examples
Joining Strings with a Delimiter
The join method can be used to join multiple strings with a specified delimiter.
Example
public class JoinExample {
public static void main(String[] args) {
String result = String.join(", ", "Alice", "Bob", "Charlie");
System.out.println("Joined string: " + result);
}
}
Output:
Joined string: Alice, Bob, Charlie
Joining Strings with Different Delimiters
The join method allows you to use different delimiters to join strings.
Example
public class JoinExample {
public static void main(String[] args) {
String result = String.join(" - ", "Java", "Python", "C++");
System.out.println("Joined string: " + result);
}
}
Output:
Joined string: Java - Python - C++
Joining an Array of Strings
The join method can be used to join an array of strings with a specified delimiter.
Example
public class JoinExample {
public static void main(String[] args) {
String[] languages = {"Java", "Python", "C++"};
String result = String.join(", ", languages);
System.out.println("Joined string: " + result);
}
}
Output:
Joined string: Java, Python, C++
Joining a List of Strings
The join method can also be used to join a list of strings with a specified delimiter.
Example
import java.util.List;
public class JoinExample {
public static void main(String[] args) {
List<String> names = List.of("Alice", "Bob", "Charlie");
String result = String.join("; ", names);
System.out.println("Joined string: " + result);
}
}
Output:
Joined string: Alice; Bob; Charlie
Conclusion
The String.join() method in Java provides a convenient way to concatenate multiple strings using a specified delimiter. By understanding how to use this method, you can efficiently create delimited strings in your Java applications. Whether you are joining individual strings, arrays, or lists, the join method offers a powerful and flexible solution for these tasks.
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