mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-10-20 06:24:05 +00:00
Resolved #66
This commit is contained in:
@@ -1236,7 +1236,7 @@ public abstract class Strman {
|
||||
|
||||
|
||||
/**
|
||||
* Removes leading whitespace from string.
|
||||
* Removes leading characters from string.
|
||||
*
|
||||
* @param input The string to trim.
|
||||
* @param chars The characters to trim.
|
||||
@@ -1252,6 +1252,36 @@ public abstract class Strman {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes trailing whitespace from string.
|
||||
*
|
||||
* @param input The string to trim.
|
||||
* @return Returns the trimmed string.
|
||||
*/
|
||||
public static Optional<String> trimEnd(final String input) {
|
||||
return Optional.ofNullable(input)
|
||||
.filter(v -> !v.isEmpty())
|
||||
.map(Strman::rightTrim);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Removes trailing characters from string.
|
||||
*
|
||||
* @param input The string to trim.
|
||||
* @param chars The characters to trim.
|
||||
*
|
||||
* @return Returns the trimmed string.
|
||||
*/
|
||||
public static Optional<String> trimEnd(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) {
|
||||
if (predicate.test(value)) {
|
||||
|
||||
@@ -974,4 +974,20 @@ public class StrmanTest {
|
||||
assertThat(trimStart("-_-!abc-_-", "_", "-", "!"), is(Optional.of("abc-_-")));
|
||||
assertThat(trimStart("-_-#abc-_-", "_", "-", "!", "#"), is(Optional.of("abc-_-")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void trimEnd_shouldRemoveAllTrailingWhitespace() throws Exception {
|
||||
assertThat(trimEnd(" abc "), is(Optional.of(" abc")));
|
||||
assertThat(trimEnd("abc "), is(Optional.of("abc")));
|
||||
assertThat(trimEnd("abc"), is(Optional.of("abc")));
|
||||
assertThat(trimEnd(""), is(Optional.empty()));
|
||||
assertThat(trimEnd(null), is(Optional.empty()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void trimEnd_shouldRemoveAllTrailingSpecialCharacters() throws Exception {
|
||||
assertThat(trimEnd("-_-abc-_-", "_", "-"), is(Optional.of("-_-abc")));
|
||||
assertThat(trimEnd("-_-abc!-_-", "_", "-", "!"), is(Optional.of("-_-abc")));
|
||||
assertThat(trimEnd("-_-abc#-_-", "_", "-", "!", "#"), is(Optional.of("-_-abc")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user