diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index 530bb9b..765bc19 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -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) { diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index ce10081..e9f56b4 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -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))); + } + } \ No newline at end of file