This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new 7c7bc19f2 Accept a null delimiter in StringUtils.join(Object[],
String, int, int) (#1873)
7c7bc19f2 is described below
commit 7c7bc19f295d7057205d3f691ad983f880eedfe2
Author: alhuda <[email protected]>
AuthorDate: Sun Nov 15 18:12:32 2026 +0530
Accept a null delimiter in StringUtils.join(Object[], String, int, int)
(#1873)
The Javadoc treats a null delimiter as "" and gives
join(["a", "b", "c"], null, 0, 3) = "abc" as an example, but the
delimiter was passed unchanged to LangCollectors.joining, whose
StringJoiner throws NullPointerException. Map it through
ObjectUtils.toString first, as join(Iterator, String) already does.
---
src/main/java/org/apache/commons/lang3/StringUtils.java | 2 +-
src/test/java/org/apache/commons/lang3/StringUtilsTest.java | 4 ++++
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java
b/src/main/java/org/apache/commons/lang3/StringUtils.java
index 63916ea08..7c3e870b4 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -4720,7 +4720,7 @@ public static String join(final Object[] array, final
String delimiter) {
*/
public static String join(final Object[] array, final String delimiter,
final int startIndex, final int endIndex) {
return array != null ?
Streams.of(array).skip(startIndex).limit(Math.max(0, endIndex - startIndex))
- .collect(LangCollectors.joining(delimiter, EMPTY, EMPTY,
ObjectUtils::toString)) : null;
+
.collect(LangCollectors.joining(ObjectUtils.toString(delimiter), EMPTY, EMPTY,
ObjectUtils::toString)) : null;
}
/**
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index 21e59b617..86a14bb09 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
@@ -1177,6 +1177,10 @@ void testJoin_ArrayString_EmptyDelimiter() {
assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST, ""));
assertEquals("", StringUtils.join(MIXED_ARRAY_LIST, "", 0,
MIXED_ARRAY_LIST.length - 1));
+ assertEquals("", StringUtils.join(MIXED_ARRAY_LIST, null, 0,
MIXED_ARRAY_LIST.length - 1));
+ assertEquals(TEXT_LIST_NOSEP, StringUtils.join(ARRAY_LIST, null, 0,
ARRAY_LIST.length));
+ assertEquals("abc", StringUtils.join(new String[] { "a", "b", "c" },
null, 0, 3));
+ assertEquals("", StringUtils.join(new String[] { "a", "b", "c" },
null, 2, 2));
}
@ParameterizedTest