List转字符串去除[]和空格
List直接通过toString()方法转换成字符串后每个逗号后都有一个空格,如果没有处理的话就会出问题,有哪些方式可以去除空格或者不带空格呢:
1、方法1:List.toString方法
通过toString方法转换成字符串后把所有的空格替换掉
list.toString().replace("[","").replace("]","").replace(" ","")
list.toString().replace("[","").replace("]","").replaceAll("\\s+","")
2、方法2:String.join()方法(推荐)
String.join(",",list)
3、方法3:Collectors.join()方法
list.stream().collect(Collectors.joining(","))
4、方法4:for循环获取
通过for循环去除list的元素拼接成字符串