Convert int[] to comma-separated string

后端 未结 6 729
情深已故
情深已故 2021-01-12 23:45

How can I convert int[] to comma-separated String in Java?

int[] intArray = {234, 808, 342};

Result I want:

\"         


        
6条回答
  •  一生所求
    2021-01-13 00:24

    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"
    

提交回复
热议问题