Skip to content

Commit 61ca9fc

Browse files
committed
Resolved shekhargulati#66
1 parent bacbe46 commit 61ca9fc

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

src/main/java/strman/Strman.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,7 @@ public static Optional<String> trimStart(final String input) {
12361236

12371237

12381238
/**
1239-
* Removes leading whitespace from string.
1239+
* Removes leading characters from string.
12401240
*
12411241
* @param input The string to trim.
12421242
* @param chars The characters to trim.
@@ -1252,6 +1252,36 @@ public static Optional<String> trimStart(final String input, String... chars) {
12521252
});
12531253
}
12541254

1255+
/**
1256+
* Removes trailing whitespace from string.
1257+
*
1258+
* @param input The string to trim.
1259+
* @return Returns the trimmed string.
1260+
*/
1261+
public static Optional<String> trimEnd(final String input) {
1262+
return Optional.ofNullable(input)
1263+
.filter(v -> !v.isEmpty())
1264+
.map(Strman::rightTrim);
1265+
}
1266+
1267+
1268+
/**
1269+
* Removes trailing characters from string.
1270+
*
1271+
* @param input The string to trim.
1272+
* @param chars The characters to trim.
1273+
*
1274+
* @return Returns the trimmed string.
1275+
*/
1276+
public static Optional<String> trimEnd(final String input, String... chars) {
1277+
return Optional.ofNullable(input)
1278+
.filter(v -> !v.isEmpty())
1279+
.map(v -> {
1280+
String pattern = String.format("[%s]+$", join(chars, "\\"));
1281+
return v.replaceAll(pattern, "");
1282+
});
1283+
}
1284+
12551285

12561286
private static void validate(String value, Predicate<String> predicate, final Supplier<String> supplier) {
12571287
if (predicate.test(value)) {

src/test/java/strman/StrmanTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,4 +974,20 @@ public void trimStart_shouldRemoveSpecialCharactersAtStart() throws Exception {
974974
assertThat(trimStart("-_-!abc-_-", "_", "-", "!"), is(Optional.of("abc-_-")));
975975
assertThat(trimStart("-_-#abc-_-", "_", "-", "!", "#"), is(Optional.of("abc-_-")));
976976
}
977+
978+
@Test
979+
public void trimEnd_shouldRemoveAllTrailingWhitespace() throws Exception {
980+
assertThat(trimEnd(" abc "), is(Optional.of(" abc")));
981+
assertThat(trimEnd("abc "), is(Optional.of("abc")));
982+
assertThat(trimEnd("abc"), is(Optional.of("abc")));
983+
assertThat(trimEnd(""), is(Optional.empty()));
984+
assertThat(trimEnd(null), is(Optional.empty()));
985+
}
986+
987+
@Test
988+
public void trimEnd_shouldRemoveAllTrailingSpecialCharacters() throws Exception {
989+
assertThat(trimEnd("-_-abc-_-", "_", "-"), is(Optional.of("-_-abc")));
990+
assertThat(trimEnd("-_-abc!-_-", "_", "-", "!"), is(Optional.of("-_-abc")));
991+
assertThat(trimEnd("-_-abc#-_-", "_", "-", "!", "#"), is(Optional.of("-_-abc")));
992+
}
977993
}

0 commit comments

Comments
 (0)