java中逗号分隔的字符串和List相互转换

时间:2018-12-28 10:43:22   收藏:0   阅读:11881

1、将逗号分隔的字符串转换为List

String str = "a,b,c";  

List<String> result = Arrays.asList(str.split(","));

2、将List转换为逗号分隔的字符串

(1) 利用Guava的Joiner

List<String> list = new ArrayList<String>();  
list.add("a");  
list.add("b");  
list.add("c");  

String str = Joiner.on(",").join(list);  

(2)利用Apache Commons的StringUtils

List<String> list = new ArrayList<String>();  
list.add("a");  
list.add("b");  
list.add("c");  

String str = StringUtils.join(list.toArray(), ",");  

原文:https://www.cnblogs.com/hui-blog/p/6375174.html

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!