mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-10-21 06:54:09 +00:00
Fixed #24. Added support for replace function
This commit is contained in:
@@ -756,6 +756,24 @@ public abstract class Strman {
|
||||
return Stream.generate(() -> value).limit(multiplier).collect(joining());
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace all occurrences of 'search' value to 'newvalue'. Uses String replace method.
|
||||
*
|
||||
* @param value The input
|
||||
* @param search The String to search
|
||||
* @param newValue The String to replace
|
||||
* @param caseSensitive whether search should be case sensitive or not
|
||||
* @return String replaced with 'newvalue'.
|
||||
*/
|
||||
public static String replace(final String value, final String search, final String newValue, final boolean caseSensitive) {
|
||||
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
|
||||
validate(search, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
|
||||
if (caseSensitive) {
|
||||
return value.replace(search, newValue);
|
||||
}
|
||||
return value.toLowerCase().replace(search.toLowerCase(), newValue);
|
||||
}
|
||||
|
||||
public static String decode(final String value, final int digits, final int radix) {
|
||||
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
|
||||
return Arrays
|
||||
|
||||
@@ -580,4 +580,16 @@ public class StrmanTest {
|
||||
assertThat(repeat("1", 4), equalTo("1111"));
|
||||
assertThat(repeat("1", 5), equalTo("11111"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void replace_shouldReplaceAllOccurrencesOfString() throws Exception {
|
||||
assertThat(replace("foo bar", "foo", "bar", true), equalTo("bar bar"));
|
||||
assertThat(replace("foo bar foo", "foo", "bar", true), equalTo("bar bar bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void replace_shouldReplaceAllOccurrencesOfStringCaseSensitive() throws Exception {
|
||||
assertThat(replace("FOO bar", "foo", "bar", false), equalTo("bar bar"));
|
||||
assertThat(replace("FOO bar foo", "foo", "bar", false), equalTo("bar bar bar"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user