/** * Concatenate the given String arrays into one, with overlapping array elements included twice. * * <p>The order of elements in the original arrays is preserved. * * @param array1 the first array (can be {@code null}) * @param array2 the second array (can be {@code null}) * @return the new array ({@code null} if both given arrays were {@code null}) */ public static String[] concatenateStringArrays(String[] array1, String[] array2) { if (org.springframework.util.ObjectUtils.isEmpty(array1)) { return array2; } if (org.springframework.util.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; }
/** * Turn given source String array into sorted array. * * @param array the source array * @return the sorted array (never {@code null}) */ public static String[] sortStringArray(String[] array) { if (org.springframework.util.ObjectUtils.isEmpty(array)) { return new String[0]; } Arrays.sort(array); return array; }
/** * Merge the given String arrays into one, with overlapping array elements only included once. * * <p>The order of elements in the original arrays is preserved (with the exception of overlapping * elements, which are only included on their first occurrence). * * @param array1 the first array (can be {@code null}) * @param array2 the second array (can be {@code null}) * @return the new array ({@code null} if both given arrays were {@code null}) */ public static String[] mergeStringArrays(String[] array1, String[] array2) { if (org.springframework.util.ObjectUtils.isEmpty(array1)) { return array2; } if (org.springframework.util.ObjectUtils.isEmpty(array2)) { return array1; } List<String> result = new ArrayList<String>(); result.addAll(Arrays.asList(array1)); for (String str : array2) { if (!result.contains(str)) { result.add(str); } } return toStringArray(result); }
/** * Convenience method to return a String array as a delimited (e.g. CSV) String. E.g. useful for * {@code toString()} implementations. * * @param arr the array to display * @param delim the delimiter to use (probably a ",") * @return the delimited String */ public static String arrayToDelimitedString(Object[] arr, String delim) { if (org.springframework.util.ObjectUtils.isEmpty(arr)) { return ""; } if (arr.length == 1) { return org.springframework.util.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(); }
/** * Append the given String to the given String array, returning a new array consisting of the * input array contents plus the given String. * * @param array the array to append to (can be {@code null}) * @param str the String to append * @return the new array (never {@code null}) */ public static String[] addStringToArray(String[] array, String str) { if (org.springframework.util.ObjectUtils.isEmpty(array)) { return new String[] {str}; } String[] newArr = new String[array.length + 1]; System.arraycopy(array, 0, newArr, 0, array.length); newArr[array.length] = str; return newArr; }
/** * Remove duplicate Strings from the given array. Also sorts the array, as it uses a TreeSet. * * @param array the String array * @return an array without duplicates, in natural sort order */ public static String[] removeDuplicateStrings(String[] array) { if (org.springframework.util.ObjectUtils.isEmpty(array)) { return array; } Set<String> set = new TreeSet<String>(); for (String element : array) { set.add(element); } return toStringArray(set); }
/** * Trim the elements of the given String array, calling {@code String.trim()} on each of them. * * @param array the original String array * @return the resulting array (of the same size) with trimmed elements */ public static String[] trimArrayElements(String[] array) { if (org.springframework.util.ObjectUtils.isEmpty(array)) { return new String[0]; } String[] result = new String[array.length]; for (int i = 0; i < array.length; i++) { String element = array[i]; result[i] = (element != null ? element.trim() : null); } return result; }
/** * Take an array Strings and split each element based on the given delimiter. A {@code Properties} * 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} 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} if no removal should * occur * @return a {@code Properties} instance representing the array contents, or {@code null} if the * array to process was {@code null} or empty */ public static Properties splitArrayElementsIntoProperties( String[] array, String delimiter, String charsToDelete) { if (org.springframework.util.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; }