diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index 7d68723..cdac971 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -28,7 +28,6 @@ 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; @@ -498,7 +497,7 @@ public abstract class Strman { * @param first The first String * @param second The second String * @return true if first and second are not equal false otherwise - * @deprecated use unequal instead + * @deprecated use unequal instead */ public static boolean inequal(final String first, final String second) { return !Objects.equals(first, second); @@ -721,11 +720,10 @@ 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.replaceFirst(s, "") : f; if (caseSensitive) { - return fx.apply(value, prefix); + return value.startsWith(prefix) ? value.substring(prefix.length()) : value; } - return fx.apply(value.toLowerCase(), prefix.toLowerCase()); + return value.toLowerCase().startsWith(prefix.toLowerCase()) ? value.substring(prefix.length()) : value; } /** @@ -802,7 +800,7 @@ public abstract class Strman { if (caseSensitive) { return value.replace(search, newValue); } - return value.toLowerCase().replace(search.toLowerCase(), newValue); + return Pattern.compile(search, Pattern.CASE_INSENSITIVE).matcher(value).replaceAll(Matcher.quoteReplacement(newValue)); } /** @@ -1061,7 +1059,7 @@ public abstract class Strman { public static String toDecamelize(final String value, final String chr) { String camelCasedString = toCamelCase(value); String[] words = camelCasedString.split("(?=\\p{Upper})"); - return Arrays.stream(words).map(w -> w.toLowerCase()).collect(joining(Optional.ofNullable(chr).orElse(" "))); + return Arrays.stream(words).map(String::toLowerCase).collect(joining(Optional.ofNullable(chr).orElse(" "))); } /** diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index f510324..be931e4 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -25,6 +25,7 @@ */ package strman; + import org.junit.Test; import java.util.Arrays; @@ -827,4 +828,21 @@ public class StrmanTest { } + @Test + public void removeLeft_shouldNotLowercaseWhenCaseInsensitive() throws Exception { + String result = removeLeft("This HAS A THIS IN FRONT", "THIS ", false); + assertThat(result, is("HAS A THIS IN FRONT")); + } + + @Test + public void replace_shouldNotLowercaseWhenCaseInsensitive() throws Exception { + String result = replace("One and two and THREE and Four", "and", "&", false); + assertThat(result, is("One & two & THREE & Four")); + } + + @Test + public void removeRight_shouldNotLowercaseWhenCaseInsensitive() throws Exception { + String result = removeRight("Remove the END at the end", " END", false); + assertThat(result, is("Remove the END at the")); + } } \ No newline at end of file