String.join() vs other string concatenation operations

后端 未结 3 1781
时光取名叫无心
时光取名叫无心 2021-01-01 18:20

I have quickly read over the Java8 String api documentation.

Now I am little curious about String.join() method to concat/join strings.

This kind of example

3条回答
  •  [愿得一人]
    2021-01-01 18:28

    We can use StringJoiner (Java8+)

    import java.util.StringJoiner;
    
    public class StringJoinerTest {
    
    public static final String FILESEPARATOR = ", ";
    
    public static void main(String[] args) {
    
        StringJoiner sj = new StringJoiner(FILESEPARATOR);
        sj.add("Test1");
        sj.add("Test2");
        sj.add("Test3");
        System.out.println(sj);
       }
    }
    

    Output is

    Test1, Test2, Test3
    

提交回复
热议问题