π 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
In this quick article, we’re going to look at different ways to convert an array of strings or integers to a string.
Using Arrays.toString() method
Arrays.toString() returns a string with the content of the input array. The new string created is a comma-delimited list of the array’s elements, surrounded with square brackets (“[]”):
// Using Arrays.toString()
public static String convertArrayToString(String[] strArray) {
return Arrays.toString(strArray);
}
Output:
[one, two, three]
Using StringBuilder.append() method
Let's use StringBuilder.append() method to convert from Array to String:
// using StringBuilder.append()
public static String convertArrayToStringMethod(String[] strArray) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < strArray.length; i++) {
stringBuilder.append(strArray[i]);
}
return stringBuilder.toString();
}
Output:
onetwothree
Using String.join() method
Java 8 and above offers the String.join() method that produces a new string by joining elements and separating them with the specified delimiter, in our case just empty string:
// Using String.join() method
public static String convertArrayToStringUsingStreamAPI(String[] strArray) {
String joinedString = String.join(" ", strArray);
return joinedString;
}
Output:
one two three
Using Collectors.joining() method from the Java Streams AP
// Using Stream API and Collectors
public static String convertArrayToStringUsingCollectors(String[] strArray) {
String joinedString = Arrays.stream(strArray)
.collect(Collectors.joining());
return joinedString;
}
Output:
onetwothree
Complete Example
package corejava;
import java.util.Arrays;
import java.util.stream.Collectors;
public class ArrayToStringConvertExample {
public static void main(String[] args) {
String[] strArray = {
"one",
"two",
"three"
};
System.out.println(convertArrayToString(strArray));
System.out.println(convertArrayToStringMethod(strArray));
System.out.println(convertArrayToStringUsingStreamAPI(strArray));
System.out.println(convertArrayToStringUsingCollectors(strArray));
// convert string to array
String[] strArray1 = "javaguides".split(" ");
}
// Using Arrays.toString()
public static String convertArrayToString(String[] strArray) {
return Arrays.toString(strArray);
}
// using StringBuilder.append()
public static String convertArrayToStringMethod(String[] strArray) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < strArray.length; i++) {
stringBuilder.append(strArray[i]);
}
return stringBuilder.toString();
}
// Using String.join() method
public static String convertArrayToStringUsingStreamAPI(String[] strArray) {
String joinedString = String.join(" ", strArray);
return joinedString;
}
// Using Stream API and Collectors
public static String convertArrayToStringUsingCollectors(String[] strArray) {
String joinedString = Arrays.stream(strArray)
.collect(Collectors.joining());
return joinedString;
}
}
Output:
[one, two, three]
onetwothree
one two three
onetwothree
Similar Examples [Snippet]
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
π High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
π High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
π Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
π₯ Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
π₯ Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
π Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
π₯ Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
π Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
π₯ Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
π₯ Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
π₯ Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
π Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment