【Java】【2】String和List相互转换

时间:2022-01-26 02:41:29

正文:

1,String转List

//常见的为逗号分隔
String str = "a,b,c";
List<String> list1 = Arrays.asList(str.split(",")); //[a, b, c]
List<String> list2 = Arrays.asList(str.split("")); //[a, ,, b, ,, c]

2,List转String

List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
String str1 = StringUtils.join(list, ","); //a,b,c
String str2 = StringUtils.join(list, ""); //abc

备注:参考的博客中还有其他方法,不过我觉得上面的比较简洁

参考博客:

1,如何相互转换逗号分隔的字符串和List (Java程序员日记 2015/03/06) - yywusuoweile的专栏 - CSDN博客

https://blog.csdn.net/yywusuoweile/article/details/50315377