Fixed #19. Added support for removeLeft function

This commit is contained in:
Shekhar Gulati
2016-05-21 18:55:20 +05:30
parent 0aa874fa93
commit 2812d51da4
2 changed files with 50 additions and 0 deletions
+30
View File
@@ -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
+20
View File
@@ -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")));
}
}