LANG-377 Add containsAny() methods to StringUtils

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@595929 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Niall Pemberton 2007-11-17 09:53:12 +00:00
parent 71a46589ca
commit 41d242e9ea
2 changed files with 107 additions and 2 deletions

View File

@ -40,8 +40,8 @@ import java.util.List;
* - null-safe index-of checks
* <li><b>IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut</b>
* - index-of any of a set of Strings</li>
* <li><b>ContainsOnly/ContainsNone</b>
* - does String contains only/none of these characters</li>
* <li><b>ContainsOnly/ContainsNone/ContainsAny</b>
* - does String contains only/none/any of these characters</li>
* <li><b>Substring/Left/Right/Mid</b>
* - null-safe substring extractions</li>
* <li><b>SubstringBefore/SubstringAfter/SubstringBetween</b>
@ -1137,6 +1137,78 @@ public class StringUtils {
return indexOfAny(str, searchChars.toCharArray());
}
// ContainsAny
//-----------------------------------------------------------------------
/**
* <p>Checks if the String contains any character in the given
* set of characters.</p>
*
* <p>A <code>null</code> String will return <code>false</code>.
* A <code>null</code> or zero length search array will return <code>false</code>.</p>
*
* <pre>
* StringUtils.containsAny(null, *) = false
* StringUtils.containsAny("", *) = false
* StringUtils.containsAny(*, null) = false
* StringUtils.containsAny(*, []) = false
* StringUtils.containsAny("zzabyycdxx",['z','a']) = true
* StringUtils.containsAny("zzabyycdxx",['b','y']) = true
* StringUtils.containsAny("aba", ['z']) = false
* </pre>
*
* @param str the String to check, may be null
* @param searchChars the chars to search for, may be null
* @return the <code>true</code> if any of the chars are found,
* <code>false</code> if no match or null input
* @since 2.4
*/
public static boolean containsAny(String str, char[] searchChars) {
if (str == null || str.length() == 0 ||
searchChars == null || searchChars.length == 0) {
return false;
}
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
for (int j = 0; j < searchChars.length; j++) {
if (searchChars[j] == ch) {
return true;
}
}
}
return false;
}
/**
* <p>Checks if the String contains any character in the given
* set of characters.</p>
*
* <p>A <code>null</code> String will return <code>false</code>.
* A <code>null</code> search string will return <code>false</code>.</p>
*
* <pre>
* StringUtils.containsAny(null, *) = false
* StringUtils.containsAny("", *) = false
* StringUtils.containsAny(*, null) = false
* StringUtils.containsAny(*, "") = false
* StringUtils.containsAny("zzabyycdxx", "za") = true
* StringUtils.containsAny("zzabyycdxx", "by") = true
* StringUtils.containsAny("aba","z") = false
* </pre>
*
* @param str the String to check, may be null
* @param searchChars the chars to search for, may be null
* @return the <code>true</code> if any of the chars are found,
* <code>false</code> if no match or null input
* @since 2.4
*/
public static boolean containsAny(String str, String searchChars) {
if (searchChars == null) {
return false;
} else {
return containsAny(str, searchChars.toCharArray());
}
}
// IndexOfAnyBut chars
//-----------------------------------------------------------------------
/**

View File

@ -377,6 +377,39 @@ public class StringUtilsEqualsIndexOfTest extends TestCase {
assertEquals(-1, StringUtils.indexOfAny("ab", "z"));
}
//-----------------------------------------------------------------------
public void testContainsAny_StringChararray() {
assertFalse(StringUtils.containsAny(null, (char[]) null));
assertFalse(StringUtils.containsAny(null, new char[0]));
assertFalse(StringUtils.containsAny(null, new char[] {'a','b'}));
assertFalse(StringUtils.containsAny("", (char[]) null));
assertFalse(StringUtils.containsAny("", new char[0]));
assertFalse(StringUtils.containsAny("", new char[] {'a','b'}));
assertFalse(StringUtils.containsAny("zzabyycdxx", (char[]) null));
assertFalse(StringUtils.containsAny("zzabyycdxx", new char[0]));
assertTrue(StringUtils.containsAny("zzabyycdxx", new char[] {'z','a'}));
assertTrue(StringUtils.containsAny("zzabyycdxx", new char[] {'b','y'}));
assertFalse(StringUtils.containsAny("ab", new char[] {'z'}));
}
public void testContainsAny_StringString() {
assertFalse(StringUtils.containsAny(null, (String) null));
assertFalse(StringUtils.containsAny(null, ""));
assertFalse(StringUtils.containsAny(null, "ab"));
assertFalse(StringUtils.containsAny("", (String) null));
assertFalse(StringUtils.containsAny("", ""));
assertFalse(StringUtils.containsAny("", "ab"));
assertFalse(StringUtils.containsAny("zzabyycdxx", (String) null));
assertFalse(StringUtils.containsAny("zzabyycdxx", ""));
assertTrue(StringUtils.containsAny("zzabyycdxx", "za"));
assertTrue(StringUtils.containsAny("zzabyycdxx", "by"));
assertFalse(StringUtils.containsAny("ab", "z"));
}
//-----------------------------------------------------------------------
public void testIndexOfAnyBut_StringChararray() {
assertEquals(-1, StringUtils.indexOfAnyBut(null, (char[]) null));