LANG-445 - new method StringUtils.startsWithAny(String str, String[] searchStrs)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@697715 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
fa436d849d
commit
a78f1c2b45
|
@ -5944,6 +5944,38 @@ public class StringUtils {
|
|||
return str.regionMatches(ignoreCase, 0, prefix, 0, prefix.length());
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Check if a String starts with any of an array of specified strings.</p>
|
||||
*
|
||||
* <pre>
|
||||
* StringUtils.startsWithAny(null, null) = false
|
||||
* StringUtils.startsWithAny(null, new String[] {"abc"}) = false
|
||||
* StringUtils.startsWithAny("abcxyz", null) = false
|
||||
* StringUtils.startsWithAny("abcxyz", new String[] {""}) = false
|
||||
* StringUtils.startsWithAny("abcxyz", new String[] {"abc"}) = true
|
||||
* StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true
|
||||
* </pre>
|
||||
*
|
||||
* @see java.lang.String#startsWithAny(String, String[])
|
||||
* @param string the String to check, may be null
|
||||
* @param searchStrings the Strings to find, may be null or empty
|
||||
* @return <code>true</code> if the String starts with any of the the prefixes, case insensitive, or
|
||||
* both <code>null</code>
|
||||
* @since 3.0
|
||||
*/
|
||||
public static boolean startsWithAny(String string, String[] searchStrings) {
|
||||
if (isEmpty(string) || ArrayUtils.isEmpty(searchStrings)) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < searchStrings.length; i++) {
|
||||
String searchString = searchStrings[i];
|
||||
if (StringUtils.startsWith(string, searchString)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// endsWith
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
|
|
|
@ -1838,4 +1838,14 @@ public class StringUtilsTest extends TestCase {
|
|||
assertEquals("i am a ", StringUtils.getCommonPrefix(new String[] {"i am a machine", "i am a robot"}));
|
||||
}
|
||||
|
||||
public void testStartsWithAny() {
|
||||
assertFalse(StringUtils.startsWithAny(null, null));
|
||||
assertFalse(StringUtils.startsWithAny(null, new String[] {"abc"}));
|
||||
assertFalse(StringUtils.startsWithAny("abcxyz", null));
|
||||
assertFalse(StringUtils.startsWithAny("abcxyz", new String[] {}));
|
||||
assertTrue(StringUtils.startsWithAny("abcxyz", new String[] {"abc"}));
|
||||
assertTrue(StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"}));
|
||||
assertFalse(StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abcd"}));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue