Sprint原始碼學習之StringUtils類
阿新 • • 發佈:2019-02-09
該類對字元的操作可以直接拿來封裝一下,以後方便自己直接用之/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package arthurv.java.spring.learn; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Enumeration; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Properties; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeSet; /** */ public abstract class StringUtils { private static final String FOLDER_SEPARATOR = "/"; private static final String WINDOWS_FOLDER_SEPARATOR = "\\"; private static final String TOP_PATH = ".."; private static final String CURRENT_PATH = "."; private static final char EXTENSION_SEPARATOR = '.'; //--------------------------------------------------------------------- // General convenience methods for working with Strings //--------------------------------------------------------------------- //判斷str是否為空值 public static boolean hasLength(CharSequence str) { return (str != null && str.length() > 0); } /** * 判斷字串是否有長度 * 注意CharSequence是String類的上層介面 * @param str * @return */ public static boolean hasLength(String str) { return hasLength((CharSequence) str); } /** * 判斷CharSequence是否有實際內容,空格不算 * <p><pre> * StringUtils.hasText(null) = false * StringUtils.hasText("") = false * StringUtils.hasText(" ") = false * StringUtils.hasText("12345") = true * StringUtils.hasText(" 12345 ") = true */ public static boolean hasText(CharSequence str) { //如果str為空,返回false if (!hasLength(str)) { return false; } //獲取str的長度 int strLen = str.length(); //迴圈遍歷str for (int i = 0; i < strLen; i++) { //如果在0到strlen之間,有一個不是空格,說明有內容,返回true if (!Character.isWhitespace(str.charAt(i))) { return true; } } return false; } /** * 判斷str是否是實際內容,純空格組成的str返回false */ public static boolean hasText(String str) { return hasText((CharSequence) str); } /** *檢測CharSequence是否有空白字元 */ public static boolean containsWhitespace(CharSequence str) { //如果長度為0,則返回false if (!hasLength(str)) { return false; } int strLen = str.length(); //迴圈遍歷str for (int i = 0; i < strLen; i++) { //如果在0到strLen之間有空白符,則返回true if (Character.isWhitespace(str.charAt(i))) { return true; } } return false; } /** *判斷給定的字串str是否含有空白符 */ public static boolean containsWhitespace(String str) { return containsWhitespace((CharSequence) str); } /** * 去掉str開頭和結尾的空白符 */ public static String trimWhitespace(String str) { //如果沒有長度,則放回str if (!hasLength(str)) { return str; } StringBuilder sb = new StringBuilder(str); //如果sb.charAt(0)是空白符的話,刪除該空白符 while (sb.length() > 0 && Character.isWhitespace(sb.charAt(0))) { sb.deleteCharAt(0); } //如果末尾是空白符的話,也刪除該空白符 while (sb.length() > 0 && Character.isWhitespace(sb.charAt(sb.length() - 1))) { sb.deleteCharAt(sb.length() - 1); } //返回去掉開頭結尾空白符之後的字串 return sb.toString(); } /** *刪除給定的字串中所有的空白符 */ public static String trimAllWhitespace(String str) { //如果str沒有長度,返回str if (!hasLength(str)) { return str; } StringBuilder sb = new StringBuilder(str); int index = 0; //迴圈遍歷sb while (sb.length() > index) { //如果當前位置index為空白符,則刪除之 if (Character.isWhitespace(sb.charAt(index))) { sb.deleteCharAt(index); } else { index++; } } //返回去掉空白符之後的字串 return sb.toString(); } /** *刪除掉str的開頭的空白符,如果有的話 */ public static String trimLeadingWhitespace(String str) { //如果str的長度為0,返回str if (!hasLength(str)) { return str; } StringBuilder sb = new StringBuilder(str); //如果開頭有字串,則刪除之 while (sb.length() > 0 && Character.isWhitespace(sb.charAt(0))) { sb.deleteCharAt(0); } //返回刪除開頭空白符的字串 return sb.toString(); } /** * 刪除str結尾的空白符,如果結尾是空白符的話 */ public static String trimTrailingWhitespace(String str) { //如果str的長度為0,返回str if (!hasLength(str)) { return str; } StringBuilder sb = new StringBuilder(str); //如結尾頭有字串,則刪除之 while (sb.length() > 0 && Character.isWhitespace(sb.charAt(sb.length() - 1))) { sb.deleteCharAt(sb.length() - 1); } //返回刪除開頭空白符的字串 return sb.toString(); } /** *刪除str中開頭是字元是給定字元的那個字元 */ public static String trimLeadingCharacter(String str, char leadingCharacter) { //如果str的長度為0,返回str if (!hasLength(str)) { return str; } StringBuilder sb = new StringBuilder(str); //判斷sb的開頭是否==leadingCharacter,若是就刪除,否則什麼也不做 while (sb.length() > 0 && sb.charAt(0) == leadingCharacter) { sb.deleteCharAt(0); } return sb.toString(); } /** *刪除結尾等於trailingCharacter的那個字元 */ public static String trimTrailingCharacter(String str, char trailingCharacter) { //如果str的長度為0,返回str if (!hasLength(str)) { return str; } StringBuilder sb = new StringBuilder(str); //判斷sb的開頭是否==leadingCharacter,若是就刪除,否則什麼也不做 while (sb.length() > 0 && sb.charAt(sb.length() - 1) == trailingCharacter) { sb.deleteCharAt(sb.length() - 1); } return sb.toString(); } /** *檢測str的字首是否是prefix,大小寫不敏感 */ public static boolean startsWithIgnoreCase(String str, String prefix) { if (str == null || prefix == null) { return false; } //如果是則返回true if (str.startsWith(prefix)) { return true; } //如果str小於字首,則返回false if (str.length() < prefix.length()) { return false; } //設定大小寫不明感 //把str的前面長度等於prefix的字元變小寫 String lcStr = str.substring(0, prefix.length()).toLowerCase(); //把prefix變小寫 String lcPrefix = prefix.toLowerCase(); //判斷 return lcStr.equals(lcPrefix); } /** *檢測str的字尾是否是prefix,大小寫不敏感 */ public static boolean endsWithIgnoreCase(String str, String suffix) { if (str == null || suffix == null) { return false; } //如果字尾是suffix,返回true if (str.endsWith(suffix)) { return true; } if (str.length() < suffix.length()) { return false; } //設定大小寫不敏感 String lcStr = str.substring(str.length() - suffix.length()).toLowerCase(); String lcSuffix = suffix.toLowerCase(); return lcStr.equals(lcSuffix); } /** * 判斷給定的str中是否有在位置index處存在子序列subString */ public static boolean substringMatch(CharSequence str, int index, CharSequence substring) { for (int j = 0; j < substring.length(); j++) { int i = index + j; //如果i>=str.length說明str字串自index到最後的長度小於subString //str.charAt(i) != substring.charAt(j),如果當前j位置字元和str中i位置字元不相等 if (i >= str.length() || str.charAt(i) != substring.charAt(j)) { return false; } } return true; } /** *檢測str中出現sub子字串的個數. */ public static int countOccurrencesOf(String str, String sub) { //邊界處理 if (str == null || sub == null || str.length() == 0 || sub.length() == 0) { return 0; } //計數器 int count = 0; //記錄當前位置 int pos = 0; int idx; //indexOf(String str,int fromIndex)str - 要搜尋的子字串。 //fromIndex - 開始搜尋的索引位置 //如果含有此sub,則計數器+1 while ((idx = str.indexOf(sub, pos)) != -1) { ++count; //下一個開始比較的位置 pos = idx + sub.length(); } //返回sub出現的個數 return count; } /** * 用newPattern來替換inString中的oldPattern */ public static String replace(String inString, String oldPattern, String newPattern) { //邊界處理 if (!hasLength(inString) || !hasLength(oldPattern) || newPattern == null) { return inString; } StringBuilder sb = new StringBuilder(); int pos = 0; //返回oldPattern在inString的位置索引 int index = inString.indexOf(oldPattern); //記錄oldPattern的長度 int patLen = oldPattern.length(); while (index >= 0) { //儲存index之前的inString子串 sb.append(inString.substring(pos, index)); //拼接新的字元(串) sb.append(newPattern); pos = index + patLen; //檢測pos之後是否還有oldPattern,如果有繼續替換 index = inString.indexOf(oldPattern, pos); } //拼接pos之後的字串 sb.append(inString.substring(pos)); // remember to append any characters to the right of a match return sb.toString(); } /** *刪除inString中符合pattern要求的字元(串) * 實現方法是:把inString中符合pattern的字元(串)替換成“”從而實現刪除 */ public static String delete(String inString, String pattern) { return replace(inString, pattern, ""); } /** * 到此可以發先StringBuilder的強大作用 * 刪除inString中在charsToDelete中存在的字元 * 例如 * inString = "abddfkjfd"; * charsToDelete = "cdjf"; * 則處理後的inString = "abk" */ public static String deleteAny(String inString, String charsToDelete) { //邊界處理 if (!hasLength(inString) || !hasLength(charsToDelete)) { return inString; } //字元構造器 StringBuilder sb = new StringBuilder(); //迴圈遍歷inString,判斷每個字元是否在charsToDelete中 for (int i = 0; i < inString.length(); i++) { //獲取當前位置i的字元c char c = inString.charAt(i); //如果charsToDelete中不包含c,則拼接到sb中 if (charsToDelete.indexOf(c) == -1) { sb.append(c); } } //返回處理過的字串 return sb.toString(); } //--------------------------------------------------------------------- // Convenience methods for working with formatted Strings //--------------------------------------------------------------------- /** * 用單引號把非空的str括起來,例如str == "hello" 那麼返回的將是‘hello’ */ public static String quote(String str) { return (str != null ? "'" + str + "'" : null); } /** * 如果給定的物件是String型別,則呼叫quote方法處理,否則什麼都不做原樣返回 */ public static Object quoteIfString(Object obj) { return (obj instanceof String ? quote((String) obj) : obj); } /** * Unqualify a string qualified by a '.' dot character. For example, * "this.name.is.qualified", returns "qualified". * @param qualifiedName the qualified name */ public static String unqualify(String qualifiedName) { return unqualify(qualifiedName, '.'); } /** * 獲取給定的字串中,最後一個滿足分隔符separator之後字串, * 例如 qualifiedName = "this:name:is:qualified" * separator = ':' * 那麼處理過後的字串就是 qualified */ public static String unqualify(String qualifiedName, char separator) { return qualifiedName.substring(qualifiedName.lastIndexOf(separator) + 1); } /** *設定首字母為大寫 */ public static String capitalize(String str) { return changeFirstCharacterCase(str, true); } /** *設定str首字母為小寫 */ public static String uncapitalize(String str) { return changeFirstCharacterCase(str, false); } private static String changeFirstCharacterCase(String str, boolean capitalize) { if (str == null || str.length() == 0) { return str; } StringBuilder sb = new StringBuilder(str.length()); if (capitalize) {//如果首字母要求大寫的話 sb.append(Character.toUpperCase(str.charAt(0))); } else { //否則首字母設定為小寫 sb.append(Character.toLowerCase(str.charAt(0))); } //拼接首字母剩下的字串 sb.append(str.substring(1)); return sb.toString(); } /** * 獲得給用路徑path中的檔名 * 例如 "mypath/myfile.txt" -> "myfile.txt". */ public static String getFilename(String path) { //邊界處理 if (path == null) { return null; } //獲得path中最後一個檔案分隔符‘/’的位置 int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR); //如果沒有分隔符,說明給定的就是檔名,直接返回即可,否則返回分隔符剩下的字元 return (separatorIndex != -1 ? path.substring(separatorIndex + 1) : path); } /** *獲得檔名的副檔名,也就是格式 * e.g. "mypath/myfile.txt" -> "txt". */ public static String getFilenameExtension(String path) { //邊界處理 if (path == null) { return null; } //獲得最後一個‘.’的位置 int extIndex = path.lastIndexOf(EXTENSION_SEPARATOR); if (extIndex == -1) { return null; } //找到最後一個檔案分隔符‘/’的位置 int folderIndex = path.lastIndexOf(FOLDER_SEPARATOR); //如果folderIndex在extIndex的右邊,返回null if (folderIndex > extIndex) { return null; } //返回‘.’之後的子字串 return path.substring(extIndex + 1); } /** *過濾掉檔案的副檔名 * 例如. "mypath/myfile.txt" -> "mypath/myfile". */ public static String stripFilenameExtension(String path) { //邊界處理 if (path == null) { return null; } //獲得最後一個‘.’的位置 int extIndex = path.lastIndexOf(EXTENSION_SEPARATOR); if (extIndex == -1) { return path; } //找到最後一個檔案分隔符‘/’的位置 int folderIndex = path.lastIndexOf(FOLDER_SEPARATOR); //如果folderIndex在extIndex的右邊,path是檔案路徑,沒有副檔名可言,直接原樣返回 if (folderIndex > extIndex) { return path; } //返回濾掉副檔名之後的子字串 return path.substring(0, extIndex); } /** * 該方法的作用如下 * 如果path = "/hello/world/ relativePtah = "java" * 經過處理後返回 /hello/world/java * 如果path = "helloworld" 那麼處理後返回java * 這個方法少了空值判斷,個人覺得加上嚴謹些 */ public static String applyRelativePath(String path, String relativePath) { //找到最後個檔案分隔符的位置 int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR); if (separatorIndex != -1) {//如果有檔案分隔符 //獲得從0到最後一個分隔符之前的子字串 String newPath = path.substring(0, separatorIndex); //如果relativePath不是以檔案分隔符開頭 if (!relativePath.startsWith(FOLDER_SEPARATOR)) { //把newPath後面追加一個/ newPath += FOLDER_SEPARATOR; } //返回newPath+relativePath return newPath + relativePath; } else {//如果沒有,就返回relativePath return relativePath; } }//end /** * Normalize the path by suppressing sequences like "path/.." and * inner simple dots. * <p>The result is convenient for path comparison. For other uses, * notice that Windows separators ("\") are replaced by simple slashes. * @param path the original path * @return the normalized path */ public static String cleanPath(String path) { //邊界處理 if (path == null) { return null; } //用/地體pathToUse的\\ String pathToUse = replace(path, WINDOWS_FOLDER_SEPARATOR, FOLDER_SEPARATOR); // Strip prefix from path to analyze, to not treat it as part of the // first path element. This is necessary to correctly parse paths like // "file:core/../core/io/Resource.class", where the ".." should just // strip the first "core" directory while keeping the "file:" prefix. //找到:的位置 int prefixIndex = pathToUse.indexOf(":"); String prefix = ""; //如果:不存在 if (prefixIndex != -1) { //字首是pathToUse中從0到prefixIndex的字元,包括: prefix = pathToUse.substring(0, prefixIndex + 1); //獲得冒號之後的所有字元(串) pathToUse = pathToUse.substring(prefixIndex + 1); } if (pathToUse.startsWith(FOLDER_SEPARATOR)) {//如果pathToUse是以/開頭 //把prefix +/ prefix = prefix + FOLDER_SEPARATOR; //過濾掉開頭的/ pathToUse = pathToUse.substring(1); } String[] pathArray = delimitedListToStringArray(pathToUse, FOLDER_SEPARATOR); List<String> pathElements = new LinkedList<String>(); int tops = 0; for (int i = pathArray.length - 1; i >= 0; i--) { String element = pathArray[i]; if (CURRENT_PATH.equals(element)) { // Points to current directory - drop it. } else if (TOP_PATH.equals(element)) { // Registering top path found. tops++; } else { if (tops > 0) { // Merging path element with element corresponding to top path. tops--; } else { // Normal path element found. pathElements.add(0, element); } } } // Remaining top paths need to be retained. for (int i = 0; i < tops; i++) { pathElements.add(0, TOP_PATH); } return prefix + collectionToDelimitedString(pathElements, FOLDER_SEPARATOR); } /** * Compare two paths after normalization of them. * @param path1 first path for comparison * @param path2 second path for comparison * @return whether the two paths are equivalent after normalization */ public static boolean pathEquals(String path1, String path2) { return cleanPath(path1).equals(cleanPath(path2)); } //檢測是否是有效路徑locale的語法是locale -O 64 -a | -m | -c -k Name ... private static void validateLocalePart(String localePart) { for (int i = 0; i < localePart.length(); i++) { char ch = localePart.charAt(i); //檢測當前字元 if (ch != '_' && ch != ' ' && !Character.isLetterOrDigit(ch)) { throw new IllegalArgumentException( "Locale part \"" + localePart + "\" contains invalid characters"); } } } //--------------------------------------------------------------------- // Convenience methods for working with String arrays //--------------------------------------------------------------------- /** * Append the given String to the given String array, returning a new array * */ public static String[] addStringToArray(String[] array, String str) { //如果arry==null或者裡面沒有元素 if (ObjectUtils.isEmpty(array)) { return new String[] {str}; } //擴充套件一個新陣列 String[] newArr = new String[array.length + 1]; //把array內容複製到newArr裡面 System.arraycopy(array, 0, newArr, 0, array.length); //把str新增到陣列末尾 newArr[array.length] = str; //返回新陣列 return newArr; } /** * 合併兩個陣列,直接無條件合併,即使兩個陣列有重複的元素 * array1空則返回array2 ,array2空則返回array1 */ public static String[] concatenateStringArrays(String[] array1, String[] array2) { if (ObjectUtils.isEmpty(array1)) { return array2; } if (ObjectUtils.isEmpty(array2)) { return array1; } //建立一個新陣列 String[] newArr = new String[array1.length + array2.length]; //資料複製 System.arraycopy(array1, 0, newArr, 0, array1.length); System.arraycopy(array2, 0, newArr, array1.length, array2.length); //返回一個新陣列 return newArr; } /** *合併兩個陣列,如果兩個陣列有重複元素的話,只選擇一個合併即可 */ public static String[] mergeStringArrays(String[] array1, String[] array2) { //如果array1空的話,返回array2 if (ObjectUtils.isEmpty(array1)) { return array2; } //如果array2空的話,返回array1 if (ObjectUtils.isEmpty(array2)) { return array1; } //定義一個array連結串列 List<String> result = new ArrayList<String>(); //先裝array1 result.addAll(Arrays.asList(array1)); //把array2跟array1不同的元素裝入連結串列 for (String str : array2) { if (!result.contains(str)) { result.add(str); } } return toStringArray(result); } /** * Turn given source String array into sorted array. * @param array the source array * @return the sorted array (never <code>null</code>) */ public static String[] sortStringArray(String[] array) { if (ObjectUtils.isEmpty(array)) { return new String[0]; } Arrays.sort(array); return array; } /** * 把集合轉化為陣列 */ public static String[] toStringArray(Collection<String> collection) { //邊界處理 if (collection == null) { return null; } //toArray(T[] a)把list裡面的元素放入a中,並返回a return collection.toArray(new String[collection.size()]); } /** *把Enumeration型別轉化為陣列 */ public static String[] toStringArray(Enumeration<String> enumeration) { if (enumeration == null) { return null; } //先轉換為list List<String> list = Collections.list(enumeration); //toArray(T[] a)把list裡面的元素放入a中,並返回a return list.toArray(new String[list.size()]); } /** *選擇 字元陣列array中首部或者尾部都是空白的元素(字串),去掉其空白 */ public static String[] trimArrayElements(String[] array) { //如果array為空,則返回長度為0的陣列 if (ObjectUtils.isEmpty(array)) { return new String[0]; } //建立一個length為array.length的陣列,其實具體實現上沒這個必要 String[] result = new String[array.length]; for (int i = 0; i < array.length; i++) { //獲取當前元素 String element = array[i]; //如果當前元素不為空,則返回經過trim處理的字串 //trim()此字串移除了前導和尾部空白的副本,如果沒有前導和尾部空白,則返回此字串。 //直接array[i] = (element != null ? element.trim() : null);也可以 result[i] = (element != null ? element.trim() : null); } //返回一個新陣列 return result; } /** *去掉陣列中的重複的元素 * 方法:遍歷陣列,把元素加入set裡自動過濾掉重複的元素,由於使用set,導致處理過的陣列 * 是排好序的陣列 */ public static String[] removeDuplicateStrings(String[] array) { //如果陣列為空,直接返回array if (ObjectUtils.isEmpty(array)) { return array; } Set<String> set = new TreeSet<String>(); //迴圈遍歷陣列,把陣列元素加入到set裡 for (String element : array) { set.add(element); } //把set轉化為陣列 return toStringArray(set); } /** *把一個字串分按照delimiter分割成兩個子字串,組成陣列返回 */ public static String[] split(String toSplit, String delimiter) { //邊界處理。個人認為該邊界處理的有問題,如果toSplit不為空而delimiter為空的話,返回的最好是原來的字串組成的 //長度為一的陣列 new String[]{toSplit},可該做法直接返回了空值 if (!hasLength(toSplit) || !hasLength(delimiter)) { return null; } //獲得delimiter的位置 int offset = toSplit.indexOf(delimiter); if (offset < 0) {//此時不符合要求 return null; } //獲得在delimiter之前的子字串 String beforeDelimiter = toSplit.substring(0, offset); //獲得在delimiter之後的子字串 String afterDelimiter = toSplit.substring(offset + delimiter.length()); //組成陣列返回 return new String[] {beforeDelimiter, afterDelimiter}; } /** * Take an array Strings and split each element based on the given delimiter. * A <code>Properties</code> instance is then generated, with the left of the * delimiter providing the key, and the right of the delimiter providing the value. * <p>Will trim both the key and value before adding them to the * <code>Properties</code> instance. * @param array the array to process * @param delimiter to split each element using (typically the equals symbol) * @param charsToDelete one or more characters to remove from each element * prior to attempting the split operation (typically the quotation mark * symbol), or <code>null</code> if no removal should occur * @return a <code>Properties</code> instance representing the array contents, * or <code>null</code> if the array to process was <code>null</code> or empty */ public static Properties splitArrayElementsIntoProperties( String[] array, String delimiter, String charsToDelete) { if (ObjectUtils.isEmpty(array)) { return null; } Properties result = new Properties(); for (String element : array) { if (charsToDelete != null) { element = deleteAny(element, charsToDelete); } String[] splittedElement = split(element, delimiter); if (splittedElement == null) { continue; } result.setProperty(splittedElement[0].trim(), splittedElement[1].trim()); } return result; } /** * Tokenize the given String into a String array via a StringTokenizer. * Trims tokens and omits empty tokens. * <p>The given delimiters string is supposed to consist of any number of * delimiter characters. Each of those characters can be used to separate * tokens. A delimiter is always a single character; for multi-character * delimiters, consider using <code>delimitedListToStringArray</code> * @param str the String to tokenize * @param delimiters the delimiter characters, assembled as String * (each of those characters is individually considered as delimiter). * @return an array of the tokens * @see java.util.StringTokenizer * @see java.lang.String#trim() * @see #delimitedListToStringArray */ public static String[] tokenizeToStringArray(String str, String delimiters) { return tokenizeToStringArray(str, delimiters, true, true); } /** * Tokenize the given String into a String array via a StringTokenizer. * <p>The given delimiters string is supposed to consist of any number of * delimiter characters. Each of those characters can be used to separate * tokens. A delimiter is always a single character; for multi-character * delimiters, consider using <code>delimitedListToStringArray</code> * @param str the String to tokenize * @param delimiters the delimiter characters, assembled as String * (each of those characters is individually considered as delimiter) * @param trimTokens trim the tokens via String's <code>trim</code> * @param ignoreEmptyTokens omit empty tokens from the result array * (only applies to tokens that are empty after trimming; StringTokenizer * will not consider subsequent delimiters as token in the first place). * @return an array of the tokens (<code>null</code> if the input String * was <code>null</code>) * @see java.util.StringTokenizer * @see java.lang.String#trim() * @see #delimitedListToStringArray */ public static String[] tokenizeToStringArray( String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) { if (str == null) { return null; } StringTokenizer st = new StringTokenizer(str, delimiters); List<String> tokens = new ArrayList<String>(); while (st.hasMoreTokens()) { String token = st.nextToken(); if (trimTokens) { token = token.trim(); } if (!ignoreEmptyTokens || token.length() > 0) { tokens.add(token); } } return toStringArray(tokens); } public static String arrayToDelimitedString(Object[] arr, String delim) { //邊界處理 if (ObjectUtils.isEmpty(arr)) { return ""; } if (arr.length == 1) { //把一個物件arr[0]通過呼叫nullSafeToString轉化為String return ObjectUtils.nullSafeToString(arr[0]); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { if (i > 0) { sb.append(delim); } sb.append(arr[i]); } return sb.toString(); } }