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
This commit is contained in:
rehammuzzamil08
2022-11-13 09:08:48 +05:00
committed by GitHub
parent e5924e50c3
commit 22f24f2c46
4 changed files with 96 additions and 0 deletions

View File

@@ -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);
}
}