diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 66a2960c3..4e421d184 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -7478,6 +7478,72 @@ public class StringUtils { return str == null ? defaultStr : str; } + /** + *
Returns the first value in the array which is not blank. + * If all the values are blank or the array is {@code null} + * or empty then {@code null} is returned.
+ * + *+ * StringUtils.firstNonBlank(null, null, null) = null + * StringUtils.firstNonBlank(null, "", " ") = null + * StringUtils.firstNonBlank(null, null, " ") = null + * StringUtils.firstNonBlank("abc") = "abc" + * StringUtils.firstNonBlank(null, "xyz") = "xyz" + * StringUtils.firstNonBlank(null, "xyz", "abc") = "xyz" + * StringUtils.firstNonBlank() = null + *+ * + * @param
Returns the first value in the array which is not empty. + * If all the values are empty or the array is {@code null} + * or empty then {@code null} is returned.
+ * + *+ * StringUtils.firstNonBlank(null, null, null) = null + * StringUtils.firstNonBlank(null, "", " ") = " " + * StringUtils.firstNonBlank(null, null, "") = null + * StringUtils.firstNonBlank("abc") = "abc" + * StringUtils.firstNonBlank(null, "xyz") = "xyz" + * StringUtils.firstNonBlank(null, "xyz", "abc") = "xyz" + * StringUtils.firstNonBlank() = null + *+ * + * @param
Returns either the passed in CharSequence, or if the CharSequence is * whitespace, empty ("") or {@code null}, the value of {@code defaultStr}.
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsEmptyBlankTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsEmptyBlankTest.java index 8e872c3e7..2eb73680d 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsEmptyBlankTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsEmptyBlankTest.java @@ -16,7 +16,9 @@ */ package org.apache.commons.lang3; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import org.junit.Test; @@ -140,4 +142,30 @@ public class StringUtilsEmptyBlankTest { assertFalse(StringUtils.isAllBlank(" ", "bar")); assertFalse(StringUtils.isAllBlank("foo", "bar")); } + + @Test + public void testFirstNonBlank() { + assertNull(StringUtils.firstNonBlank()); + assertNull(StringUtils.firstNonBlank((String[]) null)); + assertNull(StringUtils.firstNonBlank(null, null, null)); + assertNull(StringUtils.firstNonBlank(null, "", " ")); + assertNull(StringUtils.firstNonBlank(null, null, " ")); + assertEquals("zz", StringUtils.firstNonBlank(null, "zz")); + assertEquals("abc", StringUtils.firstNonBlank("abc")); + assertEquals("xyz", StringUtils.firstNonBlank(null, "xyz")); + assertEquals("xyz", StringUtils.firstNonBlank(null, "xyz", "abc")); + } + + @Test + public void testFirstNonEmpty() { + assertNull(StringUtils.firstNonEmpty()); + assertNull(StringUtils.firstNonEmpty((String[]) null)); + assertNull(StringUtils.firstNonEmpty(null, null, null)); + assertEquals(" ", StringUtils.firstNonEmpty(null, "", " ")); + assertNull(StringUtils.firstNonEmpty(null, null, "")); + assertEquals("zz", StringUtils.firstNonEmpty(null, "zz")); + assertEquals("abc", StringUtils.firstNonEmpty("abc")); + assertEquals("xyz", StringUtils.firstNonEmpty(null, "xyz")); + assertEquals("xyz", StringUtils.firstNonEmpty(null, "xyz", "abc")); + } }