How to make a word comma separated? There is an option to create a StringBuilder, write "word_from_array" + "," in it. And then remove the extra comma at the end. But maybe there is some beautiful way to do this?
|
1 answer
You can use String.join :
String result = String.join(",", container); Check: http://ideone.com/9fXTJT
Well, or use StringJoiner , which String.join uses inside:
StringJoiner joiner = new StringJoiner(","); for (String s : container) joiner.add(s); String result = joiner.toString(); Check: http://ideone.com/myQarW
If you are using the Stream API, Collectors.joining .
|