How do I concatenate two strings in Java?

前端 未结 23 3020
长情又很酷
长情又很酷 2020-11-22 04:47

I am trying to concatenate strings in Java. Why isn\'t this working?

public class StackOverflowTest {  
    public static void main(String args[]) {
                


        
23条回答
  •  囚心锁ツ
    2020-11-22 05:03

    The java 8 way:

    StringJoiner sj1 = new StringJoiner(", ");
    String joined = sj1.add("one").add("two").toString();
    // one, two
    System.out.println(joined);
    
    
    StringJoiner sj2 = new StringJoiner(", ","{", "}");
    String joined2 = sj2.add("Jake").add("John").add("Carl").toString();
    // {Jake, John, Carl}
    System.out.println(joined2);
    

提交回复
热议问题