Java string concatenation new method StringJoiner

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

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

 

For more information about StringJoiner everyone we can go and see this man very well written blog I will not elaborate too much of

https://www.hollischuang.com/archives/3283

Guess you like

Origin www.cnblogs.com/hwcs/p/11465141.html