What's the best way to build a string of delimited items in Java?

前端 未结 30 2674
耶瑟儿~
耶瑟儿~ 2020-11-22 05:36

While working in a Java app, I recently needed to assemble a comma-delimited list of values to pass to another web service without knowing how many elements there would be i

30条回答
  •  星月不相逢
    2020-11-22 06:12

    In Java 8 you can use String.join():

    List list = Arrays.asList("foo", "bar", "baz");
    String joined = String.join(" and ", list); // "foo and bar and baz"
    

    Also have a look at this answer for a Stream API example.

提交回复
热议问题