diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 495e4ec8ebf..9387376c7b9 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -4721,6 +4721,82 @@ public static String join(final Iterable> iterable, final String separator) { return join(iterable.iterator(), separator); } + /** + *
Joins the elements of the provided {@code List} into a single String + * containing the provided list of elements.
+ * + *No delimiter is added before or after the list. + * Null objects or empty strings within the array are represented by + * empty strings.
+ * + *+ * StringUtils.join(null, *) = null + * StringUtils.join([], *) = "" + * StringUtils.join([null], *) = "" + * StringUtils.join(["a", "b", "c"], ';') = "a;b;c" + * StringUtils.join(["a", "b", "c"], null) = "abc" + * StringUtils.join([null, "", "a"], ';') = ";;a" + *+ * + * @param list the {@code List} of values to join together, may be null + * @param separator the separator character to use + * @param startIndex the first index to start joining from. It is + * an error to pass in an end index past the end of the list + * @param endIndex the index to stop joining from (exclusive). It is + * an error to pass in an end index past the end of the list + * @return the joined String, {@code null} if null list input + * @since 3.8 + */ + public static String join(final List> list, final char separator, final int startIndex, final int endIndex) { + if (list == null) { + return null; + } + final int noOfItems = endIndex - startIndex; + if (noOfItems <= 0) { + return EMPTY; + } + final List> subList = list.subList(startIndex, endIndex); + return join(subList.iterator(), separator); + } + + /** + *
Joins the elements of the provided {@code List} into a single String + * containing the provided list of elements.
+ * + *No delimiter is added before or after the list. + * Null objects or empty strings within the array are represented by + * empty strings.
+ * + *+ * StringUtils.join(null, *) = null + * StringUtils.join([], *) = "" + * StringUtils.join([null], *) = "" + * StringUtils.join(["a", "b", "c"], ';') = "a;b;c" + * StringUtils.join(["a", "b", "c"], null) = "abc" + * StringUtils.join([null, "", "a"], ';') = ";;a" + *+ * + * @param list the {@code List} of values to join together, may be null + * @param separator the separator character to use + * @param startIndex the first index to start joining from. It is + * an error to pass in an end index past the end of the list + * @param endIndex the index to stop joining from (exclusive). It is + * an error to pass in an end index past the end of the list + * @return the joined String, {@code null} if null list input + * @since 3.8 + */ + public static String join(final List> list, final String separator, final int startIndex, final int endIndex) { + if (list == null) { + return null; + } + final int noOfItems = endIndex - startIndex; + if (noOfItems <= 0) { + return EMPTY; + } + final List> subList = list.subList(startIndex, endIndex); + return join(subList.iterator(), separator); + } + /** *
Joins the elements of the provided varargs into a * single String containing the provided elements.
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java index c4299b8d1e3..ee3beeb656e 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java @@ -34,6 +34,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.Iterator; +import java.util.List; import java.util.Locale; import java.util.Objects; import java.util.regex.PatternSyntaxException; @@ -100,6 +101,11 @@ public String toString() { private static final char[] CHAR_PRIM_LIST = {'1', '2'}; private static final float[] FLOAT_PRIM_LIST = {1, 2}; private static final double[] DOUBLE_PRIM_LIST = {1, 2}; + private static final List