|
22 | 22 |
|
23 | 23 | import java.util.ArrayList; |
24 | 24 | import java.util.Collection; |
| 25 | +import java.util.StringJoiner; |
25 | 26 |
|
26 | 27 | import static io.microsphere.collection.CollectionUtils.isEmpty; |
27 | 28 | import static io.microsphere.collection.ListUtils.newArrayList; |
| 29 | +import static io.microsphere.util.ArrayUtils.length; |
28 | 30 | import static io.microsphere.util.ArrayUtils.ofArray; |
29 | 31 | import static io.microsphere.util.CharSequenceUtils.isEmpty; |
30 | 32 | import static io.microsphere.util.CharSequenceUtils.length; |
@@ -835,6 +837,41 @@ public static String uncapitalize(String str) { |
835 | 837 | public static String[] toStringArray(@Nullable Collection<String> collection) { |
836 | 838 | return isEmpty(collection) ? EMPTY_STRING_ARRAY : collection.toArray(EMPTY_STRING_ARRAY); |
837 | 839 | } |
| 840 | + |
| 841 | + /** |
| 842 | + * <p>Converts an array of Objects into a single String, with each element separated by the specified delimiter.</p> |
| 843 | + * |
| 844 | + * <p>A {@code null} or empty array returns an empty string. Each element is converted to a String using |
| 845 | + * {@link String#valueOf(Object)}. If the array contains only one element, that element's String representation |
| 846 | + * is returned without the delimiter.</p> |
| 847 | + * |
| 848 | + * <h3>Example Usage</h3> |
| 849 | + * <pre>{@code |
| 850 | + * StringUtils.arrayToString(null, ",") = "" |
| 851 | + * StringUtils.arrayToString(new Object[]{}, ",") = "" |
| 852 | + * StringUtils.arrayToString(new Object[]{"a"}, ",") = "a" |
| 853 | + * StringUtils.arrayToString(new Object[]{"a", "b", "c"}, ",") = "a,b,c" |
| 854 | + * StringUtils.arrayToString(new Object[]{1, 2, 3}, "-") = "1-2-3" |
| 855 | + * }</pre> |
| 856 | + * |
| 857 | + * @param values the array of Objects to convert, may be {@code null} or empty |
| 858 | + * @param delimiter the String to use as a delimiter between elements, may be {@code null} (treated as "null") |
| 859 | + * @return the joined String, or an empty string if the input array is {@code null} or empty |
| 860 | + */ |
| 861 | + public static String arrayToString(@Nullable Object[] values, String delimiter) { |
| 862 | + int length = length(values); |
| 863 | + if (length < 1) { |
| 864 | + return EMPTY_STRING; |
| 865 | + } |
| 866 | + if (length == 1) { |
| 867 | + return valueOf(values[0]); |
| 868 | + } |
| 869 | + StringJoiner sj = new StringJoiner(delimiter); |
| 870 | + for (Object elem : values) { |
| 871 | + sj.add(valueOf(elem)); |
| 872 | + } |
| 873 | + return sj.toString(); |
| 874 | + } |
838 | 875 |
|
839 | 876 | /** |
840 | 877 | * Changes the first character of the given {@code String} to uppercase or lowercase, |
|
0 commit comments