From fc0c16a3ae716fb72f47c72dcee2eaacb9d2d7c8 Mon Sep 17 00:00:00 2001 From: rehammuzzamil08 <113211350+rehammuzzamil08@users.noreply.github.com> Date: Sun, 13 Nov 2022 09:08:48 +0500 Subject: [PATCH] BAEL-5670: Remove All Non Numeric Characters but Keep the Decimal Separator in Java String (#12827) * BAEL-5670: Remove All Non Numeric Characters but Keep the Decimal Separator in Java String * BAEL-5670: Remove All Non Numeric Characters but Keep the Decimal Separator in Java String * BAEL-5670: Remove All Non Numeric Characters but Keep the Decimal Separator in Java String - Move to a new module core-java-string-apis-2 * BAEL-5670: Add README.md file * BAEL-5670: Add test cases to handle cases where decimal sepator is not a period or dot. * BAEL-5670: Remove test cases to handle cases where decimal sepator is not a period or dot. * BAEL-5670: Update ordering of modules in pom.xml --- .../core-java-string-apis-2/README.md | 5 ++ .../core-java-string-apis-2/pom.xml | 44 ++++++++++++++++++ ...dKeepDecimalSeparatorInStringUnitTest.java | 46 +++++++++++++++++++ core-java-modules/pom.xml | 1 + 4 files changed, 96 insertions(+) create mode 100644 core-java-modules/core-java-string-apis-2/README.md create mode 100644 core-java-modules/core-java-string-apis-2/pom.xml create mode 100755 core-java-modules/core-java-string-apis-2/src/test/java/com/baeldung/removenonnumericcharacters/RemoveNonNumericCharactersAndKeepDecimalSeparatorInStringUnitTest.java diff --git a/core-java-modules/core-java-string-apis-2/README.md b/core-java-modules/core-java-string-apis-2/README.md new file mode 100644 index 0000000000..9c63838268 --- /dev/null +++ b/core-java-modules/core-java-string-apis-2/README.md @@ -0,0 +1,5 @@ +## Java String APIs + +This module contains articles about string APIs. + +### Relevant Articles: diff --git a/core-java-modules/core-java-string-apis-2/pom.xml b/core-java-modules/core-java-string-apis-2/pom.xml new file mode 100644 index 0000000000..79e13e3ba5 --- /dev/null +++ b/core-java-modules/core-java-string-apis-2/pom.xml @@ -0,0 +1,44 @@ + + + 4.0.0 + core-java-string-apis-2 + 0.0.1-SNAPSHOT + core-java-string-apis-2 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + + + com.google.guava + guava + ${guava.version} + + + org.apache.commons + commons-lang3 + ${commons.lang3.version} + + + + + core-java-string-apis-2 + + + src/main/resources + true + + + + + + 3.12.0 + 31.1-jre + + diff --git a/core-java-modules/core-java-string-apis-2/src/test/java/com/baeldung/removenonnumericcharacters/RemoveNonNumericCharactersAndKeepDecimalSeparatorInStringUnitTest.java b/core-java-modules/core-java-string-apis-2/src/test/java/com/baeldung/removenonnumericcharacters/RemoveNonNumericCharactersAndKeepDecimalSeparatorInStringUnitTest.java new file mode 100755 index 0000000000..c95aaf1c06 --- /dev/null +++ b/core-java-modules/core-java-string-apis-2/src/test/java/com/baeldung/removenonnumericcharacters/RemoveNonNumericCharactersAndKeepDecimalSeparatorInStringUnitTest.java @@ -0,0 +1,46 @@ +package com.baeldung.removenonnumericcharacters; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.apache.commons.lang3.RegExUtils; +import org.junit.jupiter.api.Test; + +import com.google.common.base.CharMatcher; + +public class RemoveNonNumericCharactersAndKeepDecimalSeparatorInStringUnitTest { + + @Test + void givenAString_whenRemovingUsingRegexAndReplaceAllMethod_thenShouldGetExpectedResult() { + String s = "Testing abc123.555abc"; + s = s.replaceAll("[^\\d.]", ""); + assertEquals("123.555", s); + } + + @Test + void givenAString_whenRemovingUsingJava8Stream_thenShouldGetExpectedResult() { + String s = "Testing abc123.555abc"; + StringBuilder sb = new StringBuilder(); + s.chars() + .mapToObj(c -> (char) c) + .filter(c -> Character.isDigit(c) || c == '.') + .forEach(sb::append); + assertEquals("123.555", sb.toString()); + } + + @Test + void givenAString_whenRemovingUsingGuavaLibrary_thenShouldGetExpectedResult() { + String s = "Testing abc123.555abc"; + String result = CharMatcher.inRange('0', '9') + .or(CharMatcher.is('.')) + .retainFrom(s); + assertEquals("123.555", result); + } + + @Test + void givenAString_whenRemovingUsingApacheCommonsLibrary_thenShouldGetExpectedResult() { + String s = "Testing abc123.555abc"; + String result = RegExUtils.removeAll(s, "[^\\d.]"); + assertEquals("123.555", result); + } + +} diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 148f536792..feed295d38 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -121,6 +121,7 @@ core-java-string-algorithms core-java-string-algorithms-2 core-java-string-apis + core-java-string-apis-2 core-java-string-conversions core-java-string-conversions-2 core-java-string-operations