From ba7020d594683276b98ae65e1a5cb9c4ecdae059 Mon Sep 17 00:00:00 2001 From: Shekhar Gulati Date: Sat, 21 May 2016 19:06:07 +0530 Subject: [PATCH] Fixed #20. Added support for `removeNonWords` function --- src/main/java/strman/Strman.java | 11 +++++++++++ src/test/java/strman/StrmanTest.java | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index b652a26..f05cea7 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -697,6 +697,17 @@ public abstract class Strman { return fx.apply(value.toLowerCase(), prefix.toLowerCase()); } + /** + * Remove all non word characters. + * + * @param value The input String + * @return String without non-word characters + */ + public static String removeNonWords(final String value) { + validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); + return value.replaceAll("[^\\w]+", ""); + } + public static String decode(final String value, final int digits, final int radix) { validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); return Arrays diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index 9bdc964..1e9e337 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -524,4 +524,16 @@ public class StrmanTest { Arrays.stream(fixture).forEach(el -> assertThat(removeLeft(el, "FOO", false), equalTo("bar"))); } + + @Test + public void removeNonWords_shouldRemoveAllNonWordsFromInputString() throws Exception { + + final String[] fixture = { + "foo bar", + "foo&bar-", + "foobar" + }; + + Arrays.stream(fixture).forEach(el -> assertThat(removeNonWords(el), equalTo("foobar"))); + } } \ No newline at end of file