diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index f05cea7..cdbbd9d 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -690,7 +690,7 @@ public abstract class Strman { public static String removeLeft(final String value, final String prefix, final boolean caseSensitive) { validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); validate(prefix, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); - BiFunction fx = (f, s) -> f.startsWith(s) ? f.replace(s, "") : f; + BiFunction fx = (f, s) -> f.startsWith(s) ? f.replaceFirst(s, "") : f; if (caseSensitive) { return fx.apply(value, prefix); } @@ -708,6 +708,31 @@ public abstract class Strman { return value.replaceAll("[^\\w]+", ""); } + /** + * Returns a new string with the 'suffix' removed, if present. Search is case sensitive. + * + * @param value The input String + * @param suffix The suffix to remove + * @return The String without suffix! + */ + public static String removeRight(final String value, final String suffix) { + return removeRight(value, suffix, true); + } + + /** + * Returns a new string with the 'suffix' removed, if present. + * + * @param value The input String + * @param suffix The suffix to remove + * @param caseSensitive whether search should be case sensitive or not + * @return The String without suffix! + */ + public static String removeRight(final String value, final String suffix, final boolean caseSensitive) { + validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); + validate(suffix, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); + return endsWith(value, suffix, caseSensitive) ? value.substring(0, value.toLowerCase().lastIndexOf(suffix.toLowerCase())) : value; + } + public static String decode(final String value, final int digits, final int radix) { validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); return Arrays diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index 1e9e337..3325aa4 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -513,6 +513,8 @@ public class StrmanTest { }; Arrays.stream(fixture).forEach(el -> assertThat(removeLeft(el, "foo"), equalTo("bar"))); + assertThat(removeLeft("barfoo", "foo"), equalTo("barfoo")); + assertThat(removeLeft("foofoo", "foo"), equalTo("foo")); } @Test @@ -536,4 +538,26 @@ public class StrmanTest { Arrays.stream(fixture).forEach(el -> assertThat(removeNonWords(el), equalTo("foobar"))); } + + @Test + public void removeRight_shouldRemoveStringFromRight() throws Exception { + final String[] fixture = { + "foobar", + "foo" + }; + + Arrays.stream(fixture).forEach(el -> assertThat(removeRight(el, "bar"), equalTo("foo"))); + assertThat(removeRight("barfoo", "bar"), equalTo("barfoo")); + assertThat(removeRight("barbar", "bar"), equalTo("bar")); + } + + @Test + public void removeRight_shouldRemoveStringFromRightCaseInSensitive() throws Exception { + final String[] fixture = { + "foobar", + "foo" + }; + + Arrays.stream(fixture).forEach(el -> assertThat(removeRight(el, "BAR", false), equalTo("foo"))); + } } \ No newline at end of file