Java 字符串数组转字符串
1、方法一:遍历
String[] arr = { "0asd", "1zxc", "2vf", "3fg", "4zz", "5yy" }; // 遍历 StringBuffer str = new StringBuffer(); for (int i=0; i<arr.length; i++) { if (i == arr.length-1) { str.append(arr[i]); } else { str.append(arr[i]); str.append(","); } }
输出结果:0asd,1zxc,2vf,3fg,4zz,5yy
2、方法二:StringUtils的join方法
(1)、输出带 , 的字符串
String[] arr = { "0asd", "1zxc", "2vf", "3fg", "4zz", "5yy" }; String string = StringUtils.join(arr, ","); System.out.println(string);
输出结果:0asd,1zxc,2vf,3fg,4zz,5yy
(2)、输出不带 , 的字符串
String[] arr = { "0asd", "1zxc", "2vf", "3fg", "4zz", "5yy" };
String string = StringUtils.join(arr);
System.out.println(string);
输出结果:0asd1zxc2vf3fg4zz5yy