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 ...
🌐
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.
🌐
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 - First, we create a Stream from an array of primitives using the Arrays.stream() method found in the java.util package. Next, we map each element to String. And finally, we concatenate the elements with our given separator.
🌐
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.
🌐
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.
🌐
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 - Learn how to turn a Java List into a comma-separated string using loops, StringBuilder, and String.join, with a close look at how each method works internally.
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.

Find elsewhere
🌐
Scaler
scaler.com › home › topics › array to string in java
Array to String In Java | Scaler Topics
June 2, 2024 - We can use java stream API to convert an array to a string. In java 8 and above versions of java we have String.join() method which creates a new string by joining elements and separating them with a specified delimiter.
🌐
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 for each element except the first one, we append the separator before appending the element itself. import java.util.Arrays; public class ArrayToStringWithJoin { public static void main(String[] args) { String[] ...
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
🌐
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 - String commaSeparatedUsingReduce = arraysAsList.stream() .reduce((x, y) -> x + "," + y) .get(); assertThat(commaSeparatedUsingReduce).isEqualTo("ONE,TWO,THREE"); Alternatively, we can also use the utility classes provided by the Apache Commons Lang library instead of the Java ones.
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("-"))
🌐
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 ...
🌐
Vultr Docs
docs.vultr.com › java › standard library › java › lang › string › join()
Java String join() - Concatenate Strings | Vultr Docs
December 27, 2024 - Explore various practical scenarios ... edge cases. Understand the signature of the method. Use the join() method to concatenate strings with a comma separator....
🌐
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.
🌐
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 { ...
🌐
Edureka Community
edureka.co › home › community › categories › java › join array elements with a separator in java
Join array elements with a separator in Java | Edureka Community
August 16, 2018 - Is there a way to join an array, i.e. perform opposite operation of what split() does. Iterating ... do it but not able to find it. Please suggest.