mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-10-20 14:34:04 +00:00
Fixed #19. Added support for removeLeft function
This commit is contained in:
@@ -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<String, String, String> 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
|
||||
|
||||
@@ -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")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user