mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-10-20 06:24:05 +00:00
Fixed #21. Added support for removeRight function
This commit is contained in:
@@ -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<String, String, String> fx = (f, s) -> f.startsWith(s) ? f.replace(s, "") : f;
|
||||
BiFunction<String, String, String> 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
|
||||
|
||||
@@ -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")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user