How to convert array of ints to String with comma separators in Java

2 Answers

0 votes
import java.util.stream.IntStream; 
import java.util.stream.Collectors;

public class MyClass {
    public static void main(String args[]) {
        int[] arr = {893, 621, 91, 3};
        String s = IntStream.of(arr).mapToObj(Integer::toString)
                                    .collect(Collectors.joining(", "));
        System.out.println(s);
    }
}
 
 
 
/*
run:
 
893, 621, 91, 3

*/

 



answered Aug 1, 2020 by avibootz
0 votes
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        int[] arr = {893, 621, 91, 3};
        String s = Arrays.toString(arr).replaceAll("\\[|\\]", "");
        
        System.out.println(s);
    }
}
 
 
 
/*
run:
 
893, 621, 91, 3

*/

 



answered Aug 1, 2020 by avibootz
...