Adding Shashi Kant Sharma's implementation of endsWithAny - LANG-614

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@997979 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2010-09-17 05:10:27 +00:00
parent fc3a096305
commit 4870fd13a2
2 changed files with 43 additions and 0 deletions

View File

@ -6310,4 +6310,35 @@ public class StringUtils {
}
return WHITESPACE_BLOCK.matcher(trim(str)).replaceAll(" ");
}
/**
* <p>Check if a String ends with any of an array of specified strings.</p>
*
* <pre>
* StringUtils.endsWithAny(null, null) = false
* StringUtils.endsWithAny(null, new String[] {"abc"}) = false
* StringUtils.endsWithAny("abcxyz", null) = false
* StringUtils.endsWithAny("abcxyz", new String[] {""}) = true
* StringUtils.endsWithAny("abcxyz", new String[] {"xyz"}) = true
* StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true
* </pre>
*
* @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 ends with any of the the prefixes, case insensitive, or
* both <code>null</code>
* @since 3.1
*/
public static boolean endsWithAny(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.endsWith(string, searchString)) {
return true;
}
}
return false;
}
}

View File

@ -135,4 +135,16 @@ public class StringUtilsStartsEndsWithTest extends TestCase {
assertFalse(StringUtils.endsWithIgnoreCase("ABCDEF", "cde"));
}
public void testEndsWithAny() {
assertFalse("StringUtils.endsWithAny(null, null)", StringUtils.endsWithAny(null, (String)null));
assertFalse("StringUtils.endsWithAny(null, new String[] {abc})", StringUtils.endsWithAny(null, new String[] {"abc"}));
assertFalse("StringUtils.endsWithAny(abcxyz, null)", StringUtils.endsWithAny("abcxyz", (String)null));
assertTrue("StringUtils.endsWithAny(abcxyz, new String[] {\"\"})", StringUtils.endsWithAny("abcxyz", new String[] {""}));
assertTrue("StringUtils.endsWithAny(abcxyz, new String[] {xyz})", StringUtils.endsWithAny("abcxyz", new String[] {"xyz"}));
assertTrue("StringUtils.endsWithAny(abcxyz, new String[] {null, xyz, abc})", StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"}));
assertFalse("StringUtils.endsWithAny(defg, new String[] {null, xyz, abc})", StringUtils.endsWithAny("defg", new String[] {null, "xyz", "abc"}));
}
}