mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-11-02 16:03:52 +00:00
Resolved #65
This commit is contained in:
@@ -1222,6 +1222,37 @@ public abstract class Strman {
|
|||||||
.get();
|
.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes leading whitespace from string.
|
||||||
|
*
|
||||||
|
* @param input The string to trim.
|
||||||
|
* @return Returns the trimmed string.
|
||||||
|
*/
|
||||||
|
public static Optional<String> trimStart(final String input) {
|
||||||
|
return Optional.ofNullable(input)
|
||||||
|
.filter(v -> !v.isEmpty())
|
||||||
|
.map(Strman::leftTrim);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes leading whitespace from string.
|
||||||
|
*
|
||||||
|
* @param input The string to trim.
|
||||||
|
* @param chars The characters to trim.
|
||||||
|
*
|
||||||
|
* @return Returns the trimmed string.
|
||||||
|
*/
|
||||||
|
public static Optional<String> trimStart(final String input, String... chars) {
|
||||||
|
return Optional.ofNullable(input)
|
||||||
|
.filter(v -> !v.isEmpty())
|
||||||
|
.map(v -> {
|
||||||
|
String pattern = String.format("^[%s]+", join(chars, "\\"));
|
||||||
|
return v.replaceAll(pattern, "");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private static void validate(String value, Predicate<String> predicate, final Supplier<String> supplier) {
|
private static void validate(String value, Predicate<String> predicate, final Supplier<String> supplier) {
|
||||||
if (predicate.test(value)) {
|
if (predicate.test(value)) {
|
||||||
throw new IllegalArgumentException(supplier.get());
|
throw new IllegalArgumentException(supplier.get());
|
||||||
|
|||||||
@@ -958,4 +958,20 @@ public class StrmanTest {
|
|||||||
public void upperFirst_shouldReturnSameStringIfFirstCharIsUpperCase() throws Exception {
|
public void upperFirst_shouldReturnSameStringIfFirstCharIsUpperCase() throws Exception {
|
||||||
assertThat(upperFirst("FRED"), is("FRED"));
|
assertThat(upperFirst("FRED"), is("FRED"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void trimStart_shouldRemoveAllWhitespaceAtStart() throws Exception {
|
||||||
|
assertThat(trimStart(" abc "), is(Optional.of("abc ")));
|
||||||
|
assertThat(trimStart("abc "), is(Optional.of("abc ")));
|
||||||
|
assertThat(trimStart("abc"), is(Optional.of("abc")));
|
||||||
|
assertThat(trimStart(""), is(Optional.empty()));
|
||||||
|
assertThat(trimStart(null), is(Optional.empty()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void trimStart_shouldRemoveSpecialCharactersAtStart() throws Exception {
|
||||||
|
assertThat(trimStart("-_-abc-_-", "_", "-"), is(Optional.of("abc-_-")));
|
||||||
|
assertThat(trimStart("-_-!abc-_-", "_", "-", "!"), is(Optional.of("abc-_-")));
|
||||||
|
assertThat(trimStart("-_-#abc-_-", "_", "-", "!", "#"), is(Optional.of("abc-_-")));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user