提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
将数组转换为默认分隔符(逗号分隔符)分隔的字符串
/** from N o w J a v a . c o m - 时 代 Java**/ //package com.nowjava; public class Main { public static void main(String[] argv) throws Exception { char[] arr = new char[] { 'b', 'o', 'o', 'k', '2', 's', '.', 'c', 'o', 'm', 'a', '1', }; System.out.println(arrayAsString(arr)); } public static String arrayAsString(final char[] arr) throws Exception { return arrayAsString(arr, null); } /*from 时 代 J a v a - N o w J a v a . c o m*/ public static String arrayAsString(final char[] arr, final String delimiter) throws Exception { final StringBuilder str = new StringBuilder(); final int lastIndx = arr.length - 1; if (lastIndx == -1) return "[]"; // str.append( "[" ); for (int i = 0;; i++) { str.append(arr[i]); if (i == lastIndx) // return str.append( "]" ).toString(); return str.toString(); str.append(delimiter != null ? delimiter.toString() : ", "); } } /** * convert the array into string separated by default delimiter which is comma separator * * @param arr * @return * @throws Exception */ public static String arrayAsString(final int[] arr) throws Exception { return arrayAsString(arr, null); } /** * convert the array into string separated by the delimiter * * @param arr * @param delimiter * @return * @throws Exception */ public static String arrayAsString(final int[] arr, final String delimiter) throws Exception { final StringBuilder str = new StringBuilder(); final