Java: convert List to a String

前端 未结 22 2602
日久生厌
日久生厌 2020-11-22 01:03

JavaScript has Array.join()

js>[\"Bill\",\"Bob\",\"Steve\"].join(\" and \")
Bill and Bob and Steve

Does Java have anything

22条回答
  •  囚心锁ツ
    2020-11-22 01:43

    Code you have is right way to do it if you want to do using JDK without any external libraries. There is no simple "one-liner" that you could use in JDK.

    If you can use external libs, I recommend that you look into org.apache.commons.lang.StringUtils class in Apache Commons library.

    An example of usage:

    List list = Arrays.asList("Bill", "Bob", "Steve");
    String joinedResult = StringUtils.join(list, " and ");
    

提交回复
热议问题