Added startsWith*, endsWith* and contains* methods in StringsUtils.

This commit is contained in:
Christian P. MOMON 2021-06-07 03:06:29 +02:00
parent 0445919fc2
commit ab69bc620c
2 changed files with 391 additions and 29 deletions

View file

@ -151,29 +151,86 @@ public class StringsUtils
{ {
boolean result; boolean result;
boolean ended = false; if ((token == null) || (strings == null))
int index = 0;
result = false;
while (!ended)
{ {
if (index < strings.length) result = false;
}
else
{
boolean ended = false;
int index = 0;
result = false;
while (!ended)
{ {
String current = strings[index]; if (index < strings.length)
if (StringUtils.contains(token, current))
{ {
ended = true; String current = strings[index];
result = true;
if (StringUtils.contains(token, current))
{
ended = true;
result = true;
}
else
{
index += 1;
}
} }
else 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; if (index < strings.size())
result = false; {
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 result;
boolean ended = false; if ((token == null) || (strings == null))
int index = 0;
result = false;
while (!ended)
{ {
if (index < strings.length) result = false;
}
else
{
boolean ended = false;
int index = 0;
result = false;
while (!ended)
{ {
String current = strings[index]; if (index < strings.length)
if (StringUtils.containsIgnoreCase(token, current))
{ {
ended = true; String current = strings[index];
result = true;
if (StringUtils.containsIgnoreCase(token, current))
{
ended = true;
result = true;
}
else
{
index += 1;
}
} }
else 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; if (index < strings.size())
result = false; {
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; 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. * 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. * Swap case.
* *

View file

@ -56,17 +56,24 @@ public class StringsUtilsTest
{ {
Assert.assertTrue(StringsUtils.containsAny("abc", "aaa", "bbb", "abc")); Assert.assertTrue(StringsUtils.containsAny("abc", "aaa", "bbb", "abc"));
Assert.assertFalse(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 contains any ignore case 01.
*/ */
@Test @Test
public void testContainsAnyIgnoreCase01() public void testContainsAnyIgnoreCatse01()
{ {
Assert.assertTrue(StringsUtils.containsAnyIgnoreCase("abc", "aaa", "bbb", "abc")); Assert.assertTrue(StringsUtils.containsAnyIgnoreCase("abc", "aaa", "bbb", "abc"));
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.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."); 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 @Test
public void testEqualsAny01() public void testEqualsAny01()
{ {
@ -649,4 +677,24 @@ public class StringsUtilsTest
Assert.assertEquals("trois", source.get(3 - 1)); 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")));
}
} }