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
This commit is contained in:
Benedikt Ritter 2014-12-27 16:20:55 +00:00
parent 23d55b0b9c
commit 9e26c7b6e9
3 changed files with 41 additions and 0 deletions

View File

@ -22,6 +22,7 @@
<body>
<release version="3.4" date="tba" description="tba">
<action issue="LANG-883" type="add" dev="britter" due-to="Daniel Stewart">Add StringUtils.containsAny(CharSequence, CharSequence...) method</action>
<action issue="LANG-1073" type="fix" dev="kinow" due-to="haiyang li">Read wrong component type of array in add in ArrayUtils</action>
<action issue="LANG-1077" type="fix" dev="kinow" due-to="haiyang li">StringUtils.ordinalIndexOf("aaaaaa", "aa", 2) != 3 in StringUtils</action>
<action issue="LANG-1072" type="fix" dev="sebb" due-to="haiyang li">Duplicated "0x" check in createBigInteger in NumberUtils</action>

View File

@ -1670,6 +1670,31 @@ public class StringUtils {
return containsAny(cs, CharSequenceUtils.toCharArray(searchChars));
}
/**
* <p>Checks if the CharSequence contains any of the CharSequences in the given array.</p>
*
* <p>
* A {@code null} CharSequence will return {@code false}. A {@code null} or zero
* length search array will return {@code false}.
* </p>
*
* @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
//-----------------------------------------------------------------------
/**

View File

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