网站建设 中企动力南昌0792,百度网站怎么做信息,电子商务网站建设题6,mi2设计公司网站一、前言关于字符串工具类StringUtils实现字符串是否包含isChinese中文、是否包含isMessyCode乱码、常规数据类型转换、替换/删除/判空、两字符串数组mergeStringArrays合并、删除trimArrayElements字符串数组指定数组元素、字符串去重removeDuplicateStrings处理、字符串集合转…一、前言关于字符串工具类StringUtils实现字符串是否包含isChinese中文、是否包含isMessyCode乱码、常规数据类型转换、替换/删除/判空、两字符串数组mergeStringArrays合并、删除trimArrayElements字符串数组指定数组元素、字符串去重removeDuplicateStrings处理、字符串集合转换等相关处理。二、代码示例import com.alibaba.fastjson.JSONObject;bbimport org.apache.commons.lang.WordUtils;bimport org.springframework.util.CollectionUtils;bimport org.springframework.util.ObjectUtils;bbimport java.nio.charset.Charset;bimport java.text.DateFormat;bimport java.text.SimpleDateFormat;bimport java.util.ArrayList;bimport java.util.Arrays;bimport java.util.Collection;bimport java.util.Collections;bimport java.util.Date;bimport java.util.Enumeration;bimport java.util.Iterator;bimport java.util.LinkedList;bimport java.util.List;bimport java.util.Locale;bimport java.util.Properties;bimport java.util.Random;bimport java.util.Set;bimport java.util.StringTokenizer;bimport java.util.TimeZone;bimport java.util.TreeSet;bimport java.util.regex.Matcher;bimport java.util.regex.Pattern;bbpublic class StringUtils {bbpublic static final Charset DEFAULT_CHARSET Charset.forName(UTF-8);bbprivate static DateFormat date_format new SimpleDateFormat(yyyy/M/d, Locale.ENGLISH);bprivate static DateFormat time_format new SimpleDateFormat(yyyy/M/d hh:mm:ss, Locale.ENGLISH);bbprivate static final String FOLDER_SEPARATOR /;bbprivate static final String WINDOWS_FOLDER_SEPARATOR \\;bbprivate static final String TOP_PATH ..;bbprivate static final String CURRENT_PATH .;bbprivate static final char EXTENSION_SEPARATOR .;bbpublic static String defFormatDate(Date date) {breturn date_format.format(date);b}bbpublic static String defFormatTime(Date date) {breturn time_format.format(date);b}bbpublic static String firstUpcase(String text) {bif (text ! null text.length() 0) {bif (text.length() 1) {breturn text.toUpperCase();b}breturn text.substring(0, 1).toUpperCase() text.substring(1); b}breturn text;b}bbpublic static String firstLowercase(String text) {bif (text ! null text.length() 0) {bif (text.length() 1) {breturn text.toLowerCase();b}breturn text.substring(0, 1).toLowerCase() text.substring(1);b}breturn text;b}bbpublic static String getter(String field) {breturn get firstUpcase(field);b}bbpublic static String setter(String field) {breturn set firstUpcase(field);b}bbpublic static String indent(int idx) {bStringBuffer result new StringBuffer(idx*4 1);bfor (int i 0; i 0.4) { b return true; b } else { b return false; b } b } bb//---------------------------------------------------------------------b// General convenience methods for working with Stringsb//---------------------------------------------------------------------bb/**b * Check whether the given String is empty.b * This method accepts any Object as an argument, comparing it tob * {code null} and the empty String. As a consequence, this methodb * will never return {code true} for a non-null non-String object.b * The Object signature is useful for general attribute handling codeb * that commonly deals with Strings but generally has to iterate overb * Objects since attributes may e.g. be primitive value objects as well.b * param str the candidate Stringb * since 3.2.1b */bpublic static boolean isEmpty(Object str) {breturn (str null || .equals(str));b}bbpublic static boolean hasEmpty(Object...strs) {bfor (Object str : strs) {bif (str null || .equals(str)) {breturn true;b}b}breturn false;b}bb/**b * Check that the given CharSequence is neither {code null} nor of length 0.b * Note: Will return {code true} for a CharSequence that purely consists of whitespace.b * b * StringUtils.hasLength(null) falseb * StringUtils.hasLength() falseb * StringUtils.hasLength( ) trueb * StringUtils.hasLength(Hello) trueb * b * param str the CharSequence to check (may be {code null})b * return {code true} if the CharSequence is not null and has lengthb * see #hasText(String)b */bpublic static boolean hasLength(CharSequence str) {breturn (str ! null str.length() 0);b}bb/**b * Check that the given String is neither {code null} nor of length 0.b * Note: Will return {code true} for a String that purely consists of whitespace.b * param str the String to check (may be {code null})b * return {code true} if the String is not null and has lengthb * see #hasLength(CharSequence)b */bpublic static boolean hasLength(String str) {breturn hasLength((CharSequence) str);b}bb/**b * Check whether the given CharSequence has actual text.b * More specifically, returns {code true} if the string not {code null},b * its length is greater than 0, and it contains at least one non-whitespace character.b * b * StringUtils.hasText(null) falseb * StringUtils.hasText() falseb * StringUtils.hasText( ) falseb * StringUtils.hasText(12345) trueb * StringUtils.hasText( 12345 ) trueb * b * param str the CharSequence to check (may be {code null})b * return {code true} if the CharSequence is not {code null},b * its length is greater than 0, and it does not contain whitespace onlyb * see Character#isWhitespaceb */bpublic static boolean hasText(CharSequence str) {bif (!hasLength(str)) {breturn false;b}bint strLen str.length();bfor (int i 0; i 0 Character.isWhitespace(sb.charAt(0))) {bsb.deleteCharAt(0);b}bwhile (sb.length() 0 Character.isWhitespace(sb.charAt(sb.length() - 1))) {bsb.deleteCharAt(sb.length() - 1);b}breturn sb.toString();b}bb/**b * Trim all whitespace from the given String:b * leading, trailing, and in between characters.b * param str the String to checkb * return the trimmed Stringb * see java.lang.Character#isWhitespaceb */bpublic static String trimAllWhitespace(String str) {bif (!hasLength(str)) {breturn str;b}bStringBuilder sb new StringBuilder(str);bint index 0;bwhile (sb.length() index) {bif (Character.isWhitespace(sb.charAt(index))) {bsb.deleteCharAt(index);b}belse {bindex;b}b}breturn sb.toString();b}bb/**b * Trim leading whitespace from the given String.b * param str the String to checkb * return the trimmed Stringb * see java.lang.Character#isWhitespaceb */bpublic static String trimLeadingWhitespace(String str) {bif (!hasLength(str)) {breturn str;b}bStringBuilder sb new StringBuilder(str);bwhile (sb.length() 0 Character.isWhitespace(sb.charAt(0))) {bsb.deleteCharAt(0);b}breturn sb.toString();b}bb/**b * Trim trailing whitespace from the given String.b * param str the String to checkb * return the trimmed Stringb * see java.lang.Character#isWhitespaceb */bpublic static String trimTrailingWhitespace(String str) {bif (!hasLength(str)) {breturn str;b}bStringBuilder sb new StringBuilder(str);bwhile (sb.length() 0 Character.isWhitespace(sb.charAt(sb.length() - 1))) {bsb.deleteCharAt(sb.length() - 1);b}breturn sb.toString();b}bb/**b * Trim all occurrences of the supplied leading character from the given String.b * param str the String to checkb * param leadingCharacter the leading character to be trimmedb * return the trimmed Stringb */bpublic static String trimLeadingCharacter(String str, char leadingCharacter) {bif (!hasLength(str)) {breturn str;b}bStringBuilder sb new StringBuilder(str);bwhile (sb.length() 0 sb.charAt(0) leadingCharacter) {bsb.deleteCharAt(0);b}breturn sb.toString();b}bb/**b * Trim all occurrences of the supplied trailing character from the given String.b * param str the String to checkb * param trailingCharacter the trailing character to be trimmedb * return the trimmed Stringb */bpublic static String trimTrailingCharacter(String str, char trailingCharacter) {bif (!hasLength(str)) {breturn str;b}bStringBuilder sb new StringBuilder(str);bwhile (sb.length() 0 sb.charAt(sb.length() - 1) trailingCharacter) {bsb.deleteCharAt(sb.length() - 1);b}breturn sb.toString();b}bbb/**b * Test if the given String starts with the specified prefix,b * ignoring upper/lower case.b * param str the String to checkb * param prefix the prefix to look forb * see java.lang.String#startsWithb */bpublic static boolean startsWithIgnoreCase(String str, String prefix) {bif (str null || prefix null) {breturn false;b}bif (str.startsWith(prefix)) {breturn true;b}bif (str.length() str.length() || str.charAt(i) ! substring.charAt(j)) {breturn false;b}b}breturn true;b}bb/**b * Count the occurrences of the substring in string s.b * param str string to search in. Return 0 if this is null.b * param sub string to search for. Return 0 if this is null.b */bpublic static int countOccurrencesOf(String str, String sub) {bif (str null || sub null || str.length() 0 || sub.length() 0) {breturn 0;b}bint count 0;bint pos 0;bint idx;bwhile ((idx str.indexOf(sub, pos)) ! -1) {bcount;bpos idx sub.length();b}breturn count;b}bb/**b * Replace all occurrences of a substring within a string withb * another string.b * param inString String to examineb * param oldPattern String to replaceb * param newPattern String to insertb * return a String with the replacementsb */bpublic static String replace(String inString, String oldPattern, String newPattern) {bif (!hasLength(inString) || !hasLength(oldPattern) || newPattern null) {breturn inString;b}bStringBuilder sb new StringBuilder();bint pos 0; // our position in the old stringbint index inString.indexOf(oldPattern);b// the index of an occurrence weve found, or -1bint patLen oldPattern.length();bwhile (index 0) {bsb.append(inString.substring(pos, index));bsb.append(newPattern);bpos index patLen;bindex inString.indexOf(oldPattern, pos);b}bsb.append(inString.substring(pos));b// remember to append any characters to the right of a matchbreturn sb.toString();b}bb/**b * Delete all occurrences of the given substring.b * param inString the original Stringb * param pattern the pattern to delete all occurrences ofb * return the resulting Stringb */bpublic static String delete(String inString, String pattern) {breturn replace(inString, pattern, );b}bb/**b * Delete any character in a given String.b * param inString the original Stringb * param charsToDelete a set of characters to delete.b * E.g. az\n will delete as, zs and new lines.b * return the resulting Stringb */bpublic static String deleteAny(String inString, String charsToDelete) {bif (!hasLength(inString) || !hasLength(charsToDelete)) {breturn inString;b}bStringBuilder sb new StringBuilder();bfor (int i 0; i myfile.txt.b * param path the file path (may be {code null})b * return the extracted filename, or {code null} if noneb */bpublic static String getFilename(String path) {bif (path null) {breturn null;b}bint separatorIndex path.lastIndexOf(FOLDER_SEPARATOR);breturn (separatorIndex ! -1 ? path.substring(separatorIndex 1) : path);b}bb/**b * Extract the filename extension from the given path,b * e.g. mypath/myfile.txt - txt.b * param path the file path (may be {code null})b * return the extracted filename extension, or {code null} if noneb */bpublic static String getFilenameExtension(String path) {bif (path null) {breturn null;b}bint extIndex path.lastIndexOf(EXTENSION_SEPARATOR);bif (extIndex -1) {breturn null;b}bint folderIndex path.lastIndexOf(FOLDER_SEPARATOR);bif (folderIndex extIndex) {breturn null;b}breturn path.substring(extIndex 1);b}bb/**b * Strip the filename extension from the given path,b * e.g. mypath/myfile.txt - mypath/myfile.b * param path the file path (may be {code null})b * return the path with stripped filename extension,b * or {code null} if noneb */bpublic static String stripFilenameExtension(String path) {bif (path null) {breturn null;b}bint extIndex path.lastIndexOf(EXTENSION_SEPARATOR);bif (extIndex -1) {breturn path;b}bint folderIndex path.lastIndexOf(FOLDER_SEPARATOR);bif (folderIndex extIndex) {breturn path;b}breturn path.substring(0, extIndex);b}bb/**b * Apply the given relative path to the given path,b * assuming standard Java folder separation (i.e. / separators).b * param path the path to start from (usually a full file path)b * param relativePath the relative path to applyb * (relative to the full file path above)b * return the full file path that results from applying the relative pathb */bpublic static String applyRelativePath(String path, String relativePath) {bint separatorIndex path.lastIndexOf(FOLDER_SEPARATOR);bif (separatorIndex ! -1) {bString newPath path.substring(0, separatorIndex);bif (!relativePath.startsWith(FOLDER_SEPARATOR)) {bnewPath FOLDER_SEPARATOR;b}breturn newPath relativePath;b}belse {breturn relativePath;b}b}bb/**b * Normalize the path by suppressing sequences like path/.. andb * inner simple dots.b * The result is convenient for path comparison. For other uses,b * notice that Windows separators (\) are replaced by simple slashes.b * param path the original pathb * return the normalized pathb */bpublic static String cleanPath(String path) {bif (path null) {breturn null;b}bString pathToUse replace(path, WINDOWS_FOLDER_SEPARATOR, FOLDER_SEPARATOR);bb// Strip prefix from path to analyze, to not treat it as part of theb// first path element. This is necessary to correctly parse paths likeb// file:core/../core/io/Resource.class, where the .. should justb// strip the first core directory while keeping the file: prefix.bint prefixIndex pathToUse.indexOf(:);bString prefix ;bif (prefixIndex ! -1) {bprefix pathToUse.substring(0, prefixIndex 1);bpathToUse pathToUse.substring(prefixIndex 1);b}bif (pathToUse.startsWith(FOLDER_SEPARATOR)) {bprefix prefix FOLDER_SEPARATOR;bpathToUse pathToUse.substring(1);b}bbString[] pathArray delimitedListToStringArray(pathToUse, FOLDER_SEPARATOR);bList pathElements new LinkedList();bint tops 0;bbfor (int i pathArray.length - 1; i 0; i--) {bString element pathArray[i];bif (CURRENT_PATH.equals(element)) {b// Points to current directory - drop it.b}belse if (TOP_PATH.equals(element)) {b// Registering top path found.btops;b}belse {bif (tops 0) {b// Merging path element with element corresponding to top path.btops--;b}belse {b// Normal path element found.bpathElements.add(0, element);b}b}b}bb// Remaining top paths need to be retained.bfor (int i 0; i This is the inverse operation of {link Locale#toString Locales toString}.b * param localeString the locale String, following {code Locales}b * {code toString()} format (en, en_UK, etc);b * also accepts spaces as separators, as an alternative to underscoresb * return a corresponding {code Locale} instanceb * throws IllegalArgumentException in case of an invalid locale specificationb */bpublic static Locale parseLocaleString(String localeString) {bString[] parts tokenizeToStringArray(localeString, _ , false, false);bString language (parts.length 0 ? parts[0] : );bString country (parts.length 1 ? parts[1] : );bvalidateLocalePart(language);bvalidateLocalePart(country);bString variant ;bif (parts.length 2) {b// There is definitely a variant, and it is everything after the countryb// code sans the separator between the country code and the variant.bint endIndexOfCountryCode localeString.lastIndexOf(country) country.length();b// Strip off any leading _ and whitespace, whats left is the variant.bvariant trimLeadingWhitespace(localeString.substring(endIndexOfCountryCode));bif (variant.startsWith(_)) {bvariant trimLeadingCharacter(variant, _);b}b}breturn (language.length() 0 ? new Locale(language, country, variant) : null);b}bbprivate static void validateLocalePart(String localePart) {bfor (int i 0; i The order of elements in the original arrays is preserved.b * param array1 the first array (can be {code null})b * param array2 the second array (can be {code null})b * return the new array ({code null} if both given arrays were {code null})b */bpublic static String[] concatenateStringArrays(String[] array1, String[] array2) {bif (ObjectUtils.isEmpty(array1)) {breturn array2;b}bif (ObjectUtils.isEmpty(array2)) {breturn array1;b}bString[] newArr new String[array1.length array2.length];bSystem.arraycopy(array1, 0, newArr, 0, array1.length);bSystem.arraycopy(array2, 0, newArr, array1.length, array2.length);breturn newArr;b}bb/**b * Merge the given String arrays into one, with overlappingb * array elements only included once.b * The order of elements in the original arrays is preservedb * (with the exception of overlapping elements, which are onlyb * included on their first occurrence).b * param array1 the first array (can be {code null})b * param array2 the second array (can be {code null})b * return the new array ({code null} if both given arrays were {code null})b */bpublic static String[] mergeStringArrays(String[] array1, String[] array2) {bif (ObjectUtils.isEmpty(array1)) {breturn array2;b}bif (ObjectUtils.isEmpty(array2)) {breturn array1;b}bList result new ArrayList();bresult.addAll(Arrays.asList(array1));bfor (String str : array2) {bif (!result.contains(str)) {bresult.add(str);b}b}breturn toStringArray(result);b}bb/**b * Turn given source String array into sorted array.b * param array the source arrayb * return the sorted array (never {code null})b */bpublic static String[] sortStringArray(String[] array) {bif (ObjectUtils.isEmpty(array)) {breturn new String[0];b}bArrays.sort(array);breturn array;b}bb/**b * Copy the given Collection into a String array.b * The Collection must contain String elements only.b * param collection the Collection to copyb * return the String array ({code null} if the passed-inb * Collection was {code null})b */bpublic static String[] toStringArray(Collection collection) {bif (collection null) {breturn null;b}breturn collection.toArray(new String[collection.size()]);b}bb/**b * Copy the given Enumeration into a String array.b * The Enumeration must contain String elements only.b * param enumeration the Enumeration to copyb * return the String array ({code null} if the passed-inb * Enumeration was {code null})b */bpublic static String[] toStringArray(Enumeration enumeration) {bif (enumeration null) {breturn null;b}bList list Collections.list(enumeration);breturn list.toArray(new String[list.size()]);b}bb/**b * Trim the elements of the given String array,b * calling {code String.trim()} on each of them.b * param array the original String arrayb * return the resulting array (of the same size) with trimmed elementsb */bpublic static String[] trimArrayElements(String[] array) {bif (ObjectUtils.isEmpty(array)) {breturn new String[0];b}bString[] result new String[array.length];bfor (int i 0; i set new TreeSet();bfor (String element : array) {bset.add(element);b}breturn toStringArray(set);b}bb/**b * Split a String at the first occurrence of the delimiter.b * Does not include the delimiter in the result.b * param toSplit the string to splitb * param delimiter to split the string up withb * return a two element array with index 0 being before the delimiter, andb * index 1 being after the delimiter (neither element includes the delimiter);b * or {code null} if the delimiter wasnt found in the given input Stringb */bpublic static String[] split(String toSplit, String delimiter) {bif (!hasLength(toSplit) || !hasLength(delimiter)) {breturn null;b}bint offset toSplit.indexOf(delimiter);bif (offset Will trim both the key and value before adding them to theb * {code Properties} instance.b * param array the array to processb * param delimiter to split each element using (typically the equals symbol)b * return a {code Properties} instance representing the array contents,b * or {code null} if the array to process was null or emptyb */bpublic static Properties splitArrayElementsIntoProperties(String[] array, String delimiter) {breturn splitArrayElementsIntoProperties(array, delimiter, null);b}bb/**b * Take an array Strings and split each element based on the given delimiter.b * A {code Properties} instance is then generated, with the left of theb * delimiter providing the key, and the right of the delimiter providing the value.b * Will trim both the key and value before adding them to theb * {code Properties} instance.b * param array the array to processb * param delimiter to split each element using (typically the equals symbol)b * param charsToDelete one or more characters to remove from each elementb * prior to attempting the split operation (typically the quotation markb * symbol), or {code null} if no removal should occurb * return a {code Properties} instance representing the array contents,b * or {code null} if the array to process was {code null} or emptyb */bpublic static Properties splitArrayElementsIntoProperties(bString[] array, String delimiter, String charsToDelete) {bbif (ObjectUtils.isEmpty(array)) {breturn null;b}bProperties result new Properties();bfor (String element : array) {bif (charsToDelete ! null) {belement deleteAny(element, charsToDelete);b}bString[] splittedElement split(element, delimiter);bif (splittedElement null) {bcontinue;b}bresult.setProperty(splittedElement[0].trim(), splittedElement[1].trim());b}breturn result;b}bb/**b * Tokenize the given String into a String array via a StringTokenizer.b * Trims tokens and omits empty tokens.b * The given delimiters string is supposed to consist of any number ofb * delimiter characters. Each of those characters can be used to separateb * tokens. A delimiter is always a single character; for multi-characterb * delimiters, consider using {code delimitedListToStringArray}b * param str the String to tokenizeb * param delimiters the delimiter characters, assembled as Stringb * (each of those characters is individually considered as delimiter).b * return an array of the tokensb * see java.util.StringTokenizerb * see String#trim()b * see #delimitedListToStringArrayb */bpublic static String[] tokenizeToStringArray(String str, String delimiters) {breturn tokenizeToStringArray(str, delimiters, true, true);b}bb/**b * Tokenize the given String into a String array via a StringTokenizer.b * The given delimiters string is supposed to consist of any number ofb * delimiter characters. Each of those characters can be used to separateb * tokens. A delimiter is always a single character; for multi-characterb * delimiters, consider using {code delimitedListToStringArray}b * param str the String to tokenizeb * param delimiters the delimiter characters, assembled as Stringb * (each of those characters is individually considered as delimiter)b * param trimTokens trim the tokens via Strings {code trim}b * param ignoreEmptyTokens omit empty tokens from the result arrayb * (only applies to tokens that are empty after trimming; StringTokenizerb * will not consider subsequent delimiters as token in the first place).b * return an array of the tokens ({code null} if the input Stringb * was {code null})b * see java.util.StringTokenizerb * see String#trim()b * see #delimitedListToStringArrayb */bpublic static String[] tokenizeToStringArray(bString str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) {bbif (str null) {breturn null;b}bStringTokenizer st new StringTokenizer(str, delimiters);bList tokens new ArrayList();bwhile (st.hasMoreTokens()) {bString token st.nextToken();bif (trimTokens) {btoken token.trim();b}bif (!ignoreEmptyTokens || token.length() 0) {btokens.add(token);b}b}breturn toStringArray(tokens);b}bb/**b * Take a String which is a delimited list and convert it to a String array.b * A single delimiter can consists of more than one character: It will stillb * be considered as single delimiter string, rather than as bunch of potentialb * delimiter characters - in contrast to {code tokenizeToStringArray}.b * param str the input Stringb * param delimiter the delimiter between elements (this is a single delimiter,b * rather than a bunch individual delimiter characters)b * return an array of the tokens in the listb * see #tokenizeToStringArrayb */bpublic static String[] delimitedListToStringArray(String str, String delimiter) {breturn delimitedListToStringArray(str, delimiter, null);b}bb/**b * Take a String which is a delimited list and convert it to a String array.b * A single delimiter can consists of more than one character: It will stillb * be considered as single delimiter string, rather than as bunch of potentialb * delimiter characters - in contrast to {code tokenizeToStringArray}.b * param str the input Stringb * param delimiter the delimiter between elements (this is a single delimiter,b * rather than a bunch individual delimiter characters)b * param charsToDelete a set of characters to delete. Useful for deleting unwantedb * line breaks: e.g. \r\n\f will delete all new lines and line feeds in a String.b * return an array of the tokens in the listb * see #tokenizeToStringArrayb */bpublic static String[] delimitedListToStringArray(String str, String delimiter, String charsToDelete) {bif (str null) {breturn new String[0];b}bif (delimiter null) {breturn new String[] {str};b}bList result new ArrayList();bif (.equals(delimiter)) {bfor (int i 0; i 0 pos str.length()) {b// Add rest of String, but not in case of empty input.bresult.add(deleteAny(str.substring(pos), charsToDelete));b}b}breturn toStringArray(result);b}bb/**b * Convert a CSV list into an array of Strings.b * param str the input Stringb * return an array of Strings, or the empty array in case of empty inputb */bpublic static String[] commaDelimitedListToStringArray(String str) {breturn delimitedListToStringArray(str, ,);b}bb/**b * Convenience method to convert a CSV string list to a set.b * Note that this will suppress duplicates.b * param str the input Stringb * return a Set of String entries in the listb */bpublic static Set commaDelimitedListToSet(String str) {bSet set new TreeSet();bString[] tokens commaDelimitedListToStringArray(str);bfor (String token : tokens) {bset.add(token);b}breturn set;b}bb/**b * Convenience method to return a Collection as a delimited (e.g. CSV)b * String. E.g. useful for {code toString()} implementations.b * param coll the Collection to displayb * param delim the delimiter to use (probably a ,)b * param prefix the String to start each element withb * param suffix the String to end each element withb * return the delimited Stringb */bpublic static String collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix) {bif (CollectionUtils.isEmpty(coll)) {breturn ;b}bStringBuilder sb new StringBuilder();bIterator it coll.iterator();bwhile (it.hasNext()) {bsb.append(prefix).append(it.next()).append(suffix);bif (it.hasNext()) {bsb.append(delim);b}b}breturn sb.toString();b}bb/**b * Convenience method to return a Collection as a delimited (e.g. CSV)b * String. E.g. useful for {code toString()} implementations.b * param coll the Collection to displayb * param delim the delimiter to use (probably a ,)b * return the delimited Stringb */bpublic static String collectionToDelimitedString(Collection coll, String delim) {breturn collectionToDelimitedString(coll, delim, , );b}bb/**b * Convenience method to return a Collection as a CSV String.b * E.g. useful for {code toString()} implementations.b * param coll the Collection to displayb * return the delimited Stringb */bpublic static String collectionToCommaDelimitedString(Collection coll) {breturn collectionToDelimitedString(coll, ,);b}bb/**b * Convenience method to return a String array as a delimited (e.g. CSV)b * String. E.g. useful for {code toString()} implementations.b * param arr the array to displayb * param delim the delimiter to use (probably a ,)b * return the delimited Stringb */bpublic static String arrayToDelimitedString(Object[] arr, String delim) {bif (ObjectUtils.isEmpty(arr)) {breturn ;b}bif (arr.length 1) {breturn ObjectUtils.nullSafeToString(arr[0]);b}bStringBuilder sb new StringBuilder();bfor (int i 0; i 0) {bsb.append(delim);b}bsb.append(arr[i]);b}breturn sb.toString();b}bb/**b * Convenience method to return a String array as a CSV String.b * E.g. useful for {code toString()} implementations.b * param arr the array to displayb * return the delimited Stringb */bpublic static String arrayToCommaDelimitedString(Object[] arr) {breturn arrayToDelimitedString(arr, ,);b}b}