代码:
package com.air.app;import java.util.ArrayList;import java.util.List;public class ListToStringTest { public static void main(String[] args) { //定义list集合 List list = new ArrayList<>(); list.add("1"); list.add("2"); list.add("3"); list.add("4"); list.add("5"); //转成以逗号隔开的字符串 String listStr = String.join(",",list); System.out.println("list转成字符串之后:"+listStr); }}
运行结果:
public static String join(CharSequence delimiter,Iterable extends CharSequence> elements)//或public static String join(CharSequence delimiter, CharSequence... elements)
参数:
delimiter:每个元素要添加的分隔符(通常是逗号)
elements:需要添加分隔符的字符串(表示被连接的数组(也可以是集合),或者是要连接的多个字符串)
来源地址:https://blog.csdn.net/liqinglonguo/article/details/131565222