Using Java 8 you can do this in a very clean way:

String.join(delimiter, elements);

This works in three ways:

1) directly specifying the elements

String joined1 = String.join(",", "a", "b", "c");

2) using arrays

String[] array = new String[] { "a", "b", "c" };
String joined2 = String.join(",", array);

3) using iterables

List<String> list = Arrays.asList(array);
String joined3 = String.join(",", list);
Answer from skiwi on Stack Overflow
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ array to string conversions
Array to String Conversions | Baeldung
September 16, 2026 - Additionally, to convert an array of integers, we can use the same approach but instead call Integer.valueOf(intArray[i]) when appending to our StringBuilder. 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:
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2016 โ€บ 03 โ€บ how-to-convert-array-to-comma-separated-string-in-java.html
How to Convert an Array to Comma Separated String in Java - Example Tutorial
June 22, 2025 - The simplest way to convert an array to comma separated String is to create a StringBuilder, iterate through the array, and add each element of the array into StringBuilder after appending the comma.
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ strings in java โ€บ array to string in java
Array to String in Java
November 27, 2023 - Maybe you're not a fan of those square brackets, or you need a specific separator between elements. In such cases, Java's String.join() and good old loops come to the rescue. Let's say you want to convert a Java string array to a string but with a twist โ€” you want to separate each element with a dash.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ array to string in java
Array to String In Java | Scaler Topics
June 2, 2024 - The stream is a series of objects ... This character is used when joining the array elements to create a string, in our example, the delimiter used is an empty string(โ€œโ€)....
๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ java-convert-array-to-string-with-separator
Java: Convert Array to String with Separator โ€” javathinking.com
We iterate through the array, and ... java.util.Arrays; public class ArrayToStringWithJoin { public static void main(String[] args) { String[] array = {"apple", "banana", "cherry"}; String separator = ", "; // Use String.join() method ...
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java array โ€บ join array of primitives with separator in java
Join Array of Primitives with Separator in Java | Baeldung
January 8, 2024 - Letโ€™s start by defining an array ... their contents: int[] intArray = {1, 2, 3, 4, 5, 6, 7, 8, 9}; char[] charArray = {'a', 'b', 'c', 'd', 'e', 'f'}; char separatorChar = '-'; String separator = String.valueOf(separato...
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ java array โ€บ join string array in java with comma or any separator
Join String Array in Java with Comma or Any Separator
3 weeks ago - The shortest way to join a String array in Java is String.join(โ€œ,โ€, array), which returns one String with the separator between every two elements and none after the last.
Find elsewhere
Top answer
1 of 14
642

Java 8+

Use String.join():

String str = String.join(",", arr);

Note that arr can also be any Iterable (such as a list), not just an array.

If you have a Stream, you can use the joining collector:

Stream.of("a", "b", "c")
      .collect(Collectors.joining(","))

Legacy (Java 7 and earlier)

StringBuilder builder = new StringBuilder();
for(String s : arr) {
    builder.append(s);
}
String str = builder.toString();

Alternatively, if you just want a "debug-style" dump of an array:

String str = Arrays.toString(arr);

Note that if you're really legacy (Java 1.4 and earlier) you'll need to replace StringBuilder there with StringBuffer.

Android

Use TextUtils.join():

String str = TextUtils.join(",", arr);

General notes

You can modify all the above examples depending on what characters, if any, you want in between strings.

DON'T use a string and just append to it with += in a loop like some of the answers show here. This sends the GC through the roof because you're creating and throwing away as many string objects as you have items in your array. For small arrays you might not really notice the difference, but for large ones it can be orders of magnitude slower.

2 of 14
106

Use Apache commons StringUtils.join(). It takes an array, as a parameter (and also has overloads for Iterable and Iterator parameters) and calls toString() on each element (if it is not null) to get each elements string representation. Each elements string representation is then joined into one string with a separator in between if one is specified:

String joinedString = StringUtils.join(new Object[]{"a", "b", 1}, "-");
System.out.println(joinedString);

Produces:

a-b-1
Top answer
1 of 4
4

Before I review your code I want to point out that Java has a String.join() method which does exactly that. Now to your code:

You use StringBuilder, good. You take care of not appending a seperator after the last and not prepending before the first element, also good. You don't handle null values though, it's your decision if you want to handle them separately or just let Java just throw a NPE. For the separator however, a null appends literally null to the String, I'd rather default to "" if separator is null. A way to get rid of the flag:

public String getArrayAsString(String[] stringArray, String separator) {
    if(stringArray == null || stringArray.length == 0) return "";

With this we already ensured that our stringArray has at least one entry. What we can do now is initialize the StringBuilder with the first String:

    StringBuilder finalString = new StringBuilder(stringArray[0]);

Since all strings after the first one need to prepend the separator, we don't need the flag anymore. Unfortunately, we can't use a foreach loop, since we have to start at index 1 as the element at index 0 is already in the StringBuilder:

    for(int i = 1; i < stringArray.length; ++i)
        finalString.append(separator).append(stringArray[i]);

And then just return what we got:

    return finalString.toString();

Whole function:

public String getArrayAsString(String[] stringArray, String separator) {
    if(stringArray == null || stringArray.length == 0) return "";

    StringBuilder finalString = new StringBuilder(stringArray[0]);

    for(int i = 1; i < stringArray.length; ++i)
        finalString.append(separator).append(stringArray[i]);

    return finalString.toString();
}

Edit:

In String.join() the parameters are swapped, so the separator comes first, the array second:

    String[] arr = new String[]{"Hello", "World"};
    System.out.println(String.join(" | ", arr));
    System.out.println(getArrayAsString(arr, " | ")); //Your function

Output:

Hello | World
Hello | World

Working code at ideone.com

2 of 4
2

I would probably drop an enchanced loop, and stick to indices:

    finalString.append(stringArray[0]);
    for (int i = 1; i < stringArray.size(); ++i) {
        finalString.append(separator);
        finalString.append(s);
    }

You may try to keep the enchanced loop by wrapping the array into a List, get a List.sublist(1)and iterate over it. However I am not sure it is more concise/elegant.

๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ convert-array-to-string-java-with-commas
Convert Array to String in Java with Commas โ€” javathinking.com
The Java 8 Stream API provides a concise way to convert an array to a comma-separated string. The Collectors.joining() method takes care of appending the elements with the specified delimiter. Extra comma at the end: When using a simple loop for concatenation, it's easy to accidentally add a comma after the last element. For example, in the following incorrect code:
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ turning-a-java-list-into-a-comma-separated-string-efc1b6c44d1b
Turning a Java List into a Comma-Separated String | Medium
June 20, 2025 - If the array fills up, StringBuilder allocates a new larger one and copies everything over. This avoids constant object churn like String does with +. Calling toString() gives you a fresh String copy of everything that was added. That final step turns the mutable character data into the fixed String object Java uses everywhere else.
Top answer
1 of 9
93

Here's what I came up with. There are several ways to do this and they depend on the tools you are using.


Using StringUtils and ArrayUtils from Commons Lang:

Use with AIint[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = StringUtils.join(ArrayUtils.toObject(arr), " - ");

You can't just use StringUtils.join(arr, " - "); because StringUtils doesn't have that overloaded version of method. Though, it has method StringUtils.join(int[], char).

Works at any Java version, from 1.2.


Using Java 8 streams:

Something like this:

Use with AIint[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = Arrays.stream(arr)
        .mapToObj(String::valueOf)
        .collect(Collectors.joining(" - "));

In fact, there are lot of variations to achieve it using streams.

Java 8's method String.join() works only with strings, so to use it you still have to convert int[] to String[].

Use with AIString[] sarr = Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new);
String result = String.join(" - ", sarr);

If you're stuck using Java 7 or earlier with no libraries, you could write your own utility method:

Use with AIpublic static String myJoin(int[] arr, String separator) {
    if (null == arr || 0 == arr.length) return "";

    StringBuilder sb = new StringBuilder(256);
    sb.append(arr[0]);

    //if (arr.length == 1) return sb.toString();

    for (int i = 1; i < arr.length; i++) sb.append(separator).append(arr[i]);

    return sb.toString();
}

Then you can do:

Use with AIint[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = myJoin(arr, " - ");
2 of 9
16

Java 8 Solution would be like this:

Use with AIStream.of(1,2,3,4).map(String::valueOf).collect(Collectors.joining("-"))
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ convert a list to a comma-separated string
Convert a List to a Comma-Separated String | Baeldung
July 2, 2025 - Third, the Collectors class provides various utility and joining() methods with different signatures. Firstly letโ€™s have a look at how to apply to a Stream the collect() method by using the Collectors.joining() method, which takes as input a CharSequence delimiter: String commaSeparatedUsingCollect = arraysAsList.stream() .collect(Collectors.joining(",")); assertThat(commaSeparatedUsingCollect).isEqualTo("ONE,TWO,THREE"); In our next example, weโ€™ll see how to use the map() method to convert each object of the list into a String and then apply the methods collect() and Collectors.joining():
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ convert-arraylist-to-comma-separated-string-in-java
Convert ArrayList to Comma Separated String in Java - GeeksforGeeks
July 23, 2025 - We can convert ArrayList to a comma-separated String using StringJoiner which is a class in java.util package which is used to construct a sequence of characters(strings) separated by a delimiter and optionally starting with a supplied prefix ...
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ strings in java โ€บ java string join() method
Java String join() Method
January 8, 2025 - Deleimeter works as a separator among the elements. Iterable which can be a list or array of elements, needs to be joined. It always returns a concatenated string of elements joined by the delimiter. import java.util.ArrayList; class Main { public static void main(String[] args) { ArrayList<String> arrayOfStrings = new ArrayList<>(); arrayOfStrings.add("Concatenating"); arrayOfStrings.add("the"); arrayOfStrings.add("array"); arrayOfStrings.add("list"); arrayOfStrings.add("elements"); arrayOfStrings.add("with"); arrayOfStrings.add("space"); arrayOfStrings.add("as"); arrayOfStrings.add("delimiter"); // joining arrayList with space between them as delimiter String joinedStr = String.join(" ", arrayOfStrings); System.out.println(joinedStr); } }
๐ŸŒ
Hostman
hostman.com โ€บ tutorials โ€บ converting-arrays-and-strings-in-java
Java Array to String and String to Array Conversion Guide
Simultaneously, strings serve as representations of character sequences, playing an indispensable role in handling textual data. In Java, strings are treated as objects, and the language furnishes a robust set of methods for the manipulation of textual information. This tutorial provides a comprehensive guide on how to convert arrays to strings and strings to arrays in Java.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-string-array-to-string
Java String Array to String | DigitalOcean
August 3, 2022 - Technical tutorials, Q&A, events โ€” This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.