diff --git a/src/fr/devinsy/strings/StringsUtils.java b/src/fr/devinsy/strings/StringsUtils.java index 61a8733..19d7bb5 100644 --- a/src/fr/devinsy/strings/StringsUtils.java +++ b/src/fr/devinsy/strings/StringsUtils.java @@ -151,29 +151,86 @@ public class StringsUtils { boolean result; - boolean ended = false; - int index = 0; - result = false; - while (!ended) + if ((token == null) || (strings == null)) { - if (index < strings.length) + result = false; + } + else + { + boolean ended = false; + int index = 0; + result = false; + while (!ended) { - String current = strings[index]; - - if (StringUtils.contains(token, current)) + if (index < strings.length) { - ended = true; - result = true; + String current = strings[index]; + + if (StringUtils.contains(token, current)) + { + ended = true; + result = true; + } + else + { + index += 1; + } } else { - index += 1; + ended = true; + result = false; } } - else + } + + // + return result; + } + + /** + * Contains any. + * + * @param token + * the token + * @param strings + * the strings + * @return true, if successful + */ + public static boolean containsAny(final String token, final StringList strings) + { + boolean result; + + if ((token == null) || (strings == null)) + { + result = false; + } + else + { + boolean ended = false; + int index = 0; + result = false; + while (!ended) { - ended = true; - result = false; + if (index < strings.size()) + { + String current = strings.get(index); + + if (StringUtils.contains(token, current)) + { + ended = true; + result = true; + } + else + { + index += 1; + } + } + else + { + ended = true; + result = false; + } } } @@ -194,29 +251,86 @@ public class StringsUtils { boolean result; - boolean ended = false; - int index = 0; - result = false; - while (!ended) + if ((token == null) || (strings == null)) { - if (index < strings.length) + result = false; + } + else + { + boolean ended = false; + int index = 0; + result = false; + while (!ended) { - String current = strings[index]; - - if (StringUtils.containsIgnoreCase(token, current)) + if (index < strings.length) { - ended = true; - result = true; + String current = strings[index]; + + if (StringUtils.containsIgnoreCase(token, current)) + { + ended = true; + result = true; + } + else + { + index += 1; + } } else { - index += 1; + ended = true; + result = false; } } - else + } + + // + return result; + } + + /** + * Contains any ignore case. + * + * @param token + * the token + * @param strings + * the strings + * @return true, if successful + */ + public static boolean containsAnyIgnoreCase(final String token, final StringList strings) + { + boolean result; + + if ((token == null) || (strings == null)) + { + result = false; + } + else + { + boolean ended = false; + int index = 0; + result = false; + while (!ended) { - ended = true; - result = false; + if (index < strings.size()) + { + String current = strings.get(index); + + if (StringUtils.containsIgnoreCase(token, current)) + { + ended = true; + result = true; + } + else + { + index += 1; + } + } + else + { + ended = true; + result = false; + } } } @@ -491,6 +605,106 @@ public class StringsUtils return result; } + /** + * Ends with any. + * + * @param token + * the token + * @param strings + * the strings + * @return true, if successful + */ + public static boolean endsWithAny(final String token, final StringList strings) + { + boolean result; + + if ((token == null) || (strings == null)) + { + result = false; + } + else + { + boolean ended = false; + int index = 0; + result = false; + while (!ended) + { + if (index < strings.size()) + { + String current = strings.get(index); + + if (StringUtils.endsWith(token, current)) + { + ended = true; + result = true; + } + else + { + index += 1; + } + } + else + { + ended = true; + result = false; + } + } + } + + // + return result; + } + + /** + * Ends with any ignore case. + * + * @param token + * the token + * @param strings + * the strings + * @return true, if successful + */ + public static boolean endsWithAnyIgnoreCase(final String token, final StringList strings) + { + boolean result; + + if ((token == null) || (strings == null)) + { + result = false; + } + else + { + boolean ended = false; + int index = 0; + result = false; + while (!ended) + { + if (index < strings.size()) + { + String current = strings.get(index); + + if (StringUtils.endsWithIgnoreCase(token, current)) + { + ended = true; + result = true; + } + else + { + index += 1; + } + } + else + { + ended = true; + result = false; + } + } + } + + // + return result; + } + /** * Checks if is contained. * @@ -1237,6 +1451,106 @@ public class StringsUtils } } + /** + * Starts with any. + * + * @param token + * the token + * @param strings + * the strings + * @return true, if successful + */ + public static boolean startsWithAny(final String token, final StringList strings) + { + boolean result; + + if ((token == null) || (strings == null)) + { + result = false; + } + else + { + boolean ended = false; + int index = 0; + result = false; + while (!ended) + { + if (index < strings.size()) + { + String current = strings.get(index); + + if (StringUtils.startsWith(token, current)) + { + ended = true; + result = true; + } + else + { + index += 1; + } + } + else + { + ended = true; + result = false; + } + } + } + + // + return result; + } + + /** + * Starts with any ignore case. + * + * @param token + * the token + * @param strings + * the strings + * @return true, if successful + */ + public static boolean startsWithAnyIgnoreCase(final String token, final StringList strings) + { + boolean result; + + if ((token == null) || (strings == null)) + { + result = false; + } + else + { + boolean ended = false; + int index = 0; + result = false; + while (!ended) + { + if (index < strings.size()) + { + String current = strings.get(index); + + if (StringUtils.startsWithIgnoreCase(token, current)) + { + ended = true; + result = true; + } + else + { + index += 1; + } + } + else + { + ended = true; + result = false; + } + } + } + + // + return result; + } + /** * Swap case. * diff --git a/test/fr/devinsy/strings/StringsUtilsTest.java b/test/fr/devinsy/strings/StringsUtilsTest.java index b306a95..8f1c9dc 100644 --- a/test/fr/devinsy/strings/StringsUtilsTest.java +++ b/test/fr/devinsy/strings/StringsUtilsTest.java @@ -56,17 +56,24 @@ public class StringsUtilsTest { Assert.assertTrue(StringsUtils.containsAny("abc", "aaa", "bbb", "abc")); Assert.assertFalse(StringsUtils.containsAny("abc", "aaa", "bbb", "aBc")); + + Assert.assertTrue(StringsUtils.containsAny("abc", new StringList("aaa", "bbb", "abc"))); + Assert.assertFalse(StringsUtils.containsAny("abc", new StringList("aaa", "bbb", "aBc"))); } /** * Test contains any ignore case 01. */ @Test - public void testContainsAnyIgnoreCase01() + public void testContainsAnyIgnoreCatse01() { Assert.assertTrue(StringsUtils.containsAnyIgnoreCase("abc", "aaa", "bbb", "abc")); Assert.assertTrue(StringsUtils.containsAnyIgnoreCase("abc", "aaa", "bbb", "aBc")); Assert.assertFalse(StringsUtils.containsAnyIgnoreCase("abc", "aaa", "bbb", "ccc")); + + Assert.assertTrue(StringsUtils.containsAnyIgnoreCase("abc", new StringList("aaa", "bbb", "abc"))); + Assert.assertTrue(StringsUtils.containsAnyIgnoreCase("abc", new StringList("aaa", "bbb", "aBc"))); + Assert.assertFalse(StringsUtils.containsAnyIgnoreCase("abc", new StringList("aaa", "bbb", "ccc"))); } /** @@ -322,6 +329,27 @@ public class StringsUtilsTest this.logger.debug("===== test done."); } + /** + * Test ends with any 01. + */ + @Test + public void testEndsWithAny01() + { + Assert.assertTrue(StringsUtils.endsWithAny("abc", new StringList("aa", "bb", "bc"))); + Assert.assertFalse(StringsUtils.endsWithAny("abc", new StringList("aa", "bb", "Bc"))); + } + + /** + * Test contains any ignore case 01. + */ + @Test + public void testEndsWithAnyIgnoreCatse01() + { + Assert.assertTrue(StringsUtils.endsWithAnyIgnoreCase("abc", new StringList("aa", "bb", "bc"))); + Assert.assertTrue(StringsUtils.endsWithAnyIgnoreCase("abc", new StringList("aa", "bb", "Bc"))); + Assert.assertFalse(StringsUtils.endsWithAnyIgnoreCase("abc", new StringList("aa", "bb", "cc"))); + } + @Test public void testEqualsAny01() { @@ -649,4 +677,24 @@ public class StringsUtilsTest Assert.assertEquals("trois", source.get(3 - 1)); } + /** + * Test starts with any 01. + */ + @Test + public void testStartsWithAny01() + { + Assert.assertTrue(StringsUtils.startsWithAny("abc", new StringList("aaa", "bb", "ab"))); + Assert.assertFalse(StringsUtils.startsWithAny("abc", new StringList("aaa", "bb", "aB"))); + } + + /** + * Test contains any ignore case 01. + */ + @Test + public void testStartsWithAnyIgnoreCatse01() + { + Assert.assertTrue(StringsUtils.startsWithAnyIgnoreCase("abc", new StringList("aa", "bb", "ab"))); + Assert.assertTrue(StringsUtils.startsWithAnyIgnoreCase("abc", new StringList("aa", "bb", "aB"))); + Assert.assertFalse(StringsUtils.startsWithAnyIgnoreCase("abc", new StringList("aa", "bb", "cc"))); + } }