Convert int[] to comma-separated string

Here's a stream version which is functionally equivalent to khelwood's, yet uses different methods.

They both create an IntStream, map each int to a String and join those with commas.

They should be pretty identical in performance too, although technically I'm calling Integer.toString(int) directly whereas he's calling String.valueOf(int) which delegates to it. On the other hand I'm calling IntStream.of() which delegates to Arrays.stream(int[]), so it's a tie.

String result = IntStream.of(intArray)
                         .mapToObj(Integer::toString)
                         .collect(Collectors.joining(", "));

This should do

String arrAsStr = Arrays.toString(intArray).replaceAll("\\[|\\]", "");

After Arrays toString, replacing the [] gives you the desired output.


You want to convert the ints to strings, and join them with commas. You can do this with streams.

int[] intArray = {234, 808, 342};
String s = Arrays.stream(intArray)
                 .mapToObj(String::valueOf) // convert each int to a string
                 .collect(Collectors.joining(", ")); // join them with ", "

Result:

"234, 808, 342"