Java how to output such characters as 1-2-3-4-5
Sorry this issue, I can not even think of a suitable title, but based on the following important jdk 1.8
1
2
3
|
StringJoiner sj =
new
StringJoiner(
"-"
,
""
,
""
);
sj.add(
"1"
).add(
"1"
).add(
"2"
);
String desiredString = sj.toString();
|
In version 1.8 added a new 'StringJoiner' method
StringJoiner java.util package is a class for a configuration separated by a delimiter character sequence (optional), and may be provided starting from the prefix and suffix to provide the end.
While this may be added after each separator with the help of a string StringBuilder class, but StringJoiner provides a simple way to achieve, without having to write a lot of code