From 9e26c7b6e9278e412178adcf6e54a20b2f7c7c70 Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Sat, 27 Dec 2014 16:20:55 +0000 Subject: [PATCH] LANG-883: Add StringUtils.containsAny(CharSequence, CharSequence...) method. Thanks to Daniel Stewart. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1648063 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 1 + .../org/apache/commons/lang3/StringUtils.java | 25 +++++++++++++++++++ .../lang3/StringUtilsEqualsIndexOfTest.java | 15 +++++++++++ 3 files changed, 41 insertions(+) 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 @@ + Add StringUtils.containsAny(CharSequence, CharSequence...) method Read wrong component type of array in add in ArrayUtils StringUtils.ordinalIndexOf("aaaaaa", "aa", 2) != 3 in StringUtils Duplicated "0x" check in createBigInteger in NumberUtils diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 6adb725ea..d0ffebf84 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -1670,6 +1670,31 @@ public class StringUtils { return containsAny(cs, CharSequenceUtils.toCharArray(searchChars)); } + /** + *

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() {