Added support for endsWith and its variants

This commit is contained in:
Shekhar Gulati
2016-05-09 15:10:20 +05:30
parent 4198488b06
commit 217ebf9361
2 changed files with 83 additions and 3 deletions
+47 -3
View File
@@ -211,9 +211,9 @@ public abstract class Strman {
/**
* Count the number of times substr appears in value
*
* @param value input
* @param subStr search string
* @param caseSensitive whether search should be case sensitive
* @param value input
* @param subStr search string
* @param caseSensitive whether search should be case sensitive
* @param allowOverlapping boolean to take into account overlapping
* @return count of times substring exists
*/
@@ -222,6 +222,50 @@ public abstract class Strman {
return countSubstr(caseSensitive ? value : value.toLowerCase(), caseSensitive ? subStr : subStr.toLowerCase(), allowOverlapping, 0L);
}
/**
* Test if value ends with search. The search is case sensitive.
*
* @param value input string
* @param search string to search
* @return true or false
*/
public static boolean endsWith(final String value, final String search) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return endsWith(value, search, value.length(), true);
}
/**
* Test if value ends with search.
*
* @param value input string
* @param search string to search
* @param caseSensitive true or false
* @return true or false
*/
public static boolean endsWith(final String value, final String search, final boolean caseSensitive) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return endsWith(value, search, value.length(), caseSensitive);
}
/**
* Test if value ends with search.
*
* @param value input string
* @param search string to search
* @param position position till which you want to search.
* @param caseSensitive true or false
* @return true or false
*/
public static boolean endsWith(final String value, final String search, final int position, final boolean caseSensitive) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
int remainingLength = position - search.length();
if (caseSensitive) {
return value.indexOf(search, remainingLength) > -1;
}
return value.toLowerCase().indexOf(search.toLowerCase(), remainingLength) > -1;
}
private static long countSubstr(String value, String subStr, boolean allowOverlapping, long count) {
int position = value.indexOf(subStr);
if (position == -1) {
+36
View File
@@ -194,4 +194,40 @@ public class StrmanTest {
assertThat(countSubstr("aaa", "aa", true, true), equalTo(2L));
}
@Test
public void endsWith_caseSensitive_ShouldBeTrueWhenStringEndsWithSearchStr() throws Exception {
String[] fixture = {
"foo bar",
"bar"
};
Arrays.stream(fixture).forEach(el -> assertTrue(endsWith(el, "bar")));
}
@Test
public void endsWith_notCaseSensitive_ShouldBeTrueWhenStringEndsWithSearchStr() throws Exception {
String[] fixture = {
"foo bar",
"bar"
};
Arrays.stream(fixture).forEach(el -> assertTrue(endsWith(el, "BAR", false)));
}
@Test
public void endsWith_caseSensitiveAtPosition_ShouldBeTrueWhenStringEndsWithSearchStr() throws Exception {
String[] fixture = {
"foo barr",
"barr"
};
Arrays.stream(fixture).forEach(el -> assertTrue(endsWith(el, "bar", el.length() - 1, true)));
}
@Test
public void endsWith_notCaseSensitiveAtPosition_ShouldBeTrueWhenStringEndsWithSearchStr() throws Exception {
String[] fixture = {
"foo barr",
"barr"
};
Arrays.stream(fixture).forEach(el -> assertTrue(endsWith(el, "BAR", el.length() - 1, false)));
}
}