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 28f86cb991
commit fc0c16a3ae
4 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,5 @@
## Java String APIs
This module contains articles about string APIs.
### Relevant Articles:

View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-string-apis-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>core-java-string-apis-2</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons.lang3.version}</version>
</dependency>
</dependencies>
<build>
<finalName>core-java-string-apis-2</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<commons.lang3.version>3.12.0</commons.lang3.version>
<guava.version>31.1-jre</guava.version>
</properties>
</project>

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

View File

@ -121,6 +121,7 @@
<module>core-java-string-algorithms</module> <module>core-java-string-algorithms</module>
<module>core-java-string-algorithms-2</module> <module>core-java-string-algorithms-2</module>
<module>core-java-string-apis</module> <module>core-java-string-apis</module>
<module>core-java-string-apis-2</module>
<module>core-java-string-conversions</module> <module>core-java-string-conversions</module>
<module>core-java-string-conversions-2</module> <module>core-java-string-conversions-2</module>
<module>core-java-string-operations</module> <module>core-java-string-operations</module>