diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 061542a4c..1f83a6729 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@
Checks if the CharSequence contains any of the CharSequences in the given array.
+ * + *+ * A {@code null} CharSequence will return {@code false}. A {@code null} or zero + * length search array will return {@code false}. + *
+ * + * @param cs The CharSequence to check, may be null + * @param searchCharSequences The array of CharSequences to search for, may be null + * @return {@code true} if any of the search CharSequences are found, {@code false} otherwise + * @since 3.4 + */ + public static boolean containsAny(CharSequence cs, CharSequence... searchCharSequences) { + if (isEmpty(cs) || ArrayUtils.isEmpty(searchCharSequences)) { + return false; + } + for (int i = 0; i < searchCharSequences.length; i++) { + if (contains(cs, searchCharSequences[i])) { + return true; + } + } + return false; + } + // IndexOfAnyBut chars //----------------------------------------------------------------------- /** diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java index 7b328b06f..99c766aaa 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java @@ -213,6 +213,21 @@ public class StringUtilsEqualsIndexOfTest { assertFalse(StringUtils.containsAny(CharU20000, CharU20001)); assertFalse(StringUtils.containsAny(CharU20001, CharU20000)); } + + @Test + public void testContainsAny_StringStringArray() { + assertFalse(StringUtils.containsAny(null, (String[]) null)); + assertFalse(StringUtils.containsAny(null, new String[0])); + assertFalse(StringUtils.containsAny(null, new String[] { "hello" })); + assertFalse(StringUtils.containsAny("", (String[]) null)); + assertFalse(StringUtils.containsAny("", new String[0])); + assertFalse(StringUtils.containsAny("", new String[] { "hello" })); + assertFalse(StringUtils.containsAny("hello, goodbye", (String[]) null)); + assertFalse(StringUtils.containsAny("hello, goodbye", new String[0])); + assertTrue(StringUtils.containsAny("hello, goodbye", new String[] { "hello", "goodbye" })); + assertTrue(StringUtils.containsAny("hello, goodbye", new String[] { "hello", "Goodbye" })); + assertFalse(StringUtils.containsAny("hello, goodbye", new String[] { "Hello", "Goodbye" })); + } @Test public void testContainsIgnoreCase_LocaleIndependence() {