Sometimes you want to convert an array of strings or integers into a single string. However, unfortunately, there is no direct way to perform this conversion in Java.
The default implementation of the toString()
method on an array only tells us about the object's type and hash code and returns something like [Ljava.lang.String;@f6f4d33
as output.
In this article, we shall look at different ways to convert an array into a string in Java.
Convert an array to a string using String.join()
The String.join()
method returns a new string composed of a set of elements joined together using the specified delimiter:
String[] fruits = {"Apple", "Orange", "Mango", "Banana"};
String str = String.join(", ", fruits);
System.out.println(str);
// Apple, Orange, Mango, Banana
You can also pass the strings that you want to join directly to the String.join()
method, as shown below:
String str = String.join(" ", "Java", "is", "awesome", "🌟");
System.out.println(str);
// Java is awesome 🌟
The String.join()
method also works for List
objects and character sequences:
List<String> animals = List.of("Fox", "Dog", "Loin", "Cow");
String str = String.join("-", animals);
System.out.println(str);
// Fox-Dog-Loin-Cow
CharSequence[] vowels = {"a", "e", "i", "o", "u"};
String str2 = String.join(",", vowels);
System.out.println(str2);
// a,e,i,o,u
Convert an array to a string using Java Streams
Java Streams API provides the Collectors.joining()
method to join strings from the Stream
using a delimiter:
String[] fruits = {"Apple", "Orange", "Mango", "Banana"};
String str = Arrays.stream(fruits).collect(Collectors.joining(", "));
System.out.println(str);
// Apple, Orange, Mango, Banana
Besides delimiter, you can also pass prefix and suffix of your choice to the Collectors.joining()
method:
String[] fruits = {"Apple", "Orange", "Mango", "Banana"};
String str = Arrays.stream(fruits)
.collect(Collectors.joining(", ", "[", "]"));
System.out.println(str);
// [Apple, Orange, Mango, Banana]
Convert an array to a string using Arrays.toString()
The Arrays.toString()
method returns a string representation of the contents of the specified array. All array's elements are joined together using a comma (,
) as a delimiter and enclosed in square brackets ([]
) as shown below:
String[] fruits = {"Apple", "Orange", "Mango", "Banana"};
String str = Arrays.toString(fruits);
System.out.println(str);
// [Apple, Orange, Mango, Banana]
The best thing about Arrays.toString()
is that it accepts both primitive and object arrays and converts them into a string:
int[] number = {1, 2, 3, 4};
System.out.println(Arrays.toString(number));
// [1, 2, 3, 4]
double[] prices = {3.46, 9.89, 4.0, 2.89};
System.out.println(Arrays.toString(prices));
// [3.46, 9.89, 4.0, 2.89]
To learn more about an integer-to-string conversion, read this article.
Convert an array to a string using StringBuilder.append()
The StringBuilder
class is used to create mutable strings in Java. It provides an append()
method to append the specified string to the sequence.
The toString()
method of the StringBuilder
class returns a string representation of the data appended.
To convert an array to a string using StringBuilder
, we have to use a loop to iterate over all array's elements and then call the append()
method to append them into the sequence:
String[] fruits = {"Apple", "Orange", "Mango", "Banana"};
StringBuilder builder = new StringBuilder();
for (int i = 0; i < fruits.length; i++) {
builder.append(fruits[i]).append(" ");
}
String str = builder.toString();
System.out.println(str);
// Apple Orange Mango Banana
Similarly, we can also convert an array of integers into a string using StringBuilder
:
int[] number = {1, 2, 3, 4};
StringBuilder builder = new StringBuilder();
for (int i = 0; i < number.length; i++) {
builder.append(number[i]).append(" ");
}
String str = builder.toString();
System.out.println(str);
// 1 2 3 4
Convert an array to a string using StringJoiner
The StringJoiner
class was introduced in Java 8, and it provides methods for combining multiple strings into a single string using the specified delimiter:
String path = new StringJoiner("/")
.add("/usr")
.add("share")
.add("projects")
.add("java11")
.add("examples").toString();
System.out.println(path);
// /usr/share/projects/java11/examples
As you can see above, the StringJoiner
class provides a very fluent way of joining strings. We can easily chain multiple calls together to build a string.
Convert an array to a string using Apache Commons Lang
Finally, the last way to convert an array of strings into a single string is the Apache Commons Lang library.
The join()
method of the StringUtils
class from Commons Lang transforms an array of strings into a single string:
String[] names = {"Atta", "Arif", "Meero", "Alex"};
String str = StringUtils.join(names, "|");
System.out.println(str);
// Atta|Arif|Meero|Alex
To convert a string back into an array in Java, read this article.
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.