diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index 87b8fa5..b652a26 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -1,6 +1,7 @@ package strman; import java.util.*; +import java.util.function.BiFunction; import java.util.function.Predicate; import java.util.function.Supplier; import java.util.regex.Matcher; @@ -667,6 +668,35 @@ public abstract class Strman { return Arrays.stream(strings).filter(str -> str != null && !str.trim().isEmpty()).toArray(String[]::new); } + /** + * Returns a new String with the prefix removed, if present. This is case sensitive. + * + * @param value The input String + * @param prefix String to remove on left + * @return The String without prefix + */ + public static String removeLeft(final String value, final String prefix) { + return removeLeft(value, prefix, true); + } + + /** + * Returns a new String with the prefix removed, if present. + * + * @param value The input String + * @param prefix String to remove on left + * @param caseSensitive ensure case sensitivity + * @return The String without prefix + */ + 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; + if (caseSensitive) { + return fx.apply(value, prefix); + } + return fx.apply(value.toLowerCase(), prefix.toLowerCase()); + } + 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 fe9a277..9bdc964 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -504,4 +504,24 @@ public class StrmanTest { assertThat(removeEmptyStrings(new String[]{"aa", "", " ", "bb", "cc", null}), arrayContaining("aa", "bb", "cc")); assertThat(removeEmptyStrings(new String[0]), emptyArray()); } + + @Test + public void removeLeft_shouldRemoveStringFromLeft() throws Exception { + final String[] fixture = { + "foobar", + "bar" + }; + + Arrays.stream(fixture).forEach(el -> assertThat(removeLeft(el, "foo"), equalTo("bar"))); + } + + @Test + public void removeLeft_shouldRemoveStringFromLeftCaseInSensitive() throws Exception { + final String[] fixture = { + "foobar", + "bar" + }; + + Arrays.stream(fixture).forEach(el -> assertThat(removeLeft(el, "FOO", false), equalTo("bar"))); + } } \ No newline at end of file