将逗号分隔的字符串转换为List
// 将逗号分隔的字符串转换为List String str = "a,b,c"; // 1.使用JDK,逗号分隔的字符串-->数组-->list List<String> result = Arrays.asList(str.split(",")); // 2.使用Apache Commons的StringUtils List<String> result1 = Arrays.asList(StringUtils.split(str, ",")); // 3.通过遍历 String[] strings = str.split(","); List<String> result2 = new ArrayList<String>(); for (String string : strings) { result2.add(string); }
将List转换为逗号分隔的字符串
// 将List转换为逗号分隔的字符串 List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); list.add("c"); // 1.使用Apache Commons的StringUtils String str1 = StringUtils.join(list.toArray(), ","); // 2.通过遍历 StringBuffer str2 = new StringBuffer(); for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) { String string = (String) iterator.next(); str2.append(string); if(iterator.hasNext()){ str2.append(","); } }
Apache Commons的StringUtils下载: http://download.csdn.net/download/xc_oo0/9988044
http://commons.apache.org/proper/commons-lang/download_lang.cgi
参考文章: https://www.cnblogs.com/hui-blog/p/6375174.html