public class of StringUtil { Private static String checkNull (String STR) { IF (STR == null ) { return "" ; } return STR; } Private static String checkToString (Object obj) { IF (obj == null ) { return " null " ; } return obj.toString (); } / * * the array elements in the specified range is connected to a string format specified @param src source array * * @Param prePrompt front prompt string, is generally empty, "" commonly used are "XXX:" * @param prefix prefix string, typically "[" * @param suffix suffix string, generally "]" * @param separator separator elements of an array, generally set by a semicolon "," * @param Start to participate in the first element of the array subscripts connection length * @param len array element is connected to participate * @return array element is connected to the character string * / Private static String concatElementsToString (Object [] the src, prePrompt String, String prefix, suffix String, String Separator, int Start, int len) { IF (the src == null ) { return "" ; } if (src.length == 0) { return ""; } if (len <= 0 ) { return ""; } if(len>src.length-start){ len=src.length-start; } prePrompt = checkNull(prePrompt); prefix = checkNull(prefix); suffix = checkNull(suffix); separator = checkNull(separator); StringBuilder strBuilder = new StringBuilder(); strBuilder.append(prePrompt); strBuilder.append(prefix); for (int i = start; i < start + len; i++) { strBuilder.append(checkToString(src[i])); strBuilder.append(separator); } int oldStringBuilderLength = strBuilder.length(); StringBuilder resultStrBuilder = new StringBuilder( strBuilder.toString().substring(0, oldStringBuilderLength - separator.length())); resultStrBuilder.append(suffix); returnresultStrBuilder.toString (); } / * * * all elements of the array is connected to a format specified string * @param src source array * @param prefix prefix string, typically "[" * @param suffix suffix string, generally "]" * @param separator separator elements of an array, generally set by a semicolon "," string * @return array element connected to * / public static string concatElementsToString (Object [] the src, string prefix, suffix string , string Separator) { return concatElementsToString (the src, null , prefix, suffix, Separator, 0 , src.length); } / * * * the elements in the specified range of the array is connected to a format specified string @param src source array * * @param prefix prefix string , usually "[" * @Param suffix suffix string, generally "]" * @param Separator separator elements of an array, generally set by a semicolon "," * @param Start the first element is connected to participate in the array subscript * @param len to participate length of the connecting element array string * @return array element connected to * / public static string concatElementsToString (Object [] the src, string prefix, suffix string, string Separator, int Start, int len) { return concatElementsToString (the src, null , prefix, suffix, Separator, start, len); } / * * elements * array index is greater than or equal to start connection to a format specified string * @param src source array * @param separator separator elements of an array, generally set by a semicolon", " * @param prefix prefix string, typically "[" * @Param suffix suffix string, generally "]" * @Param start to participate in the first element of the array subscripts connected string * @return array element connected to "* / public static String concatElementsToString (Object [] the src, String prefix, suffix String, String Separator, int Start) { return concatElementsToString (the src, null , prefix, suffix, Separator, Start, src.length- Start); } public static void main (String [] args) { String A = concatElementsToString (Constants.ISSUE_CODES, " [ " , " ] " , \ R & lt \ n- " , . 1 ,3); String b=concatElementsToString(Constants.ISSUE_CODES,"[" ,"]", "||",9); String c=concatElementsToString(Constants.ISSUE_CODES,"[" ,"]", ","); System.out.println(a); System.out.println(b); System.out.println(c); } }
Console output