BAEL-7026: Check If a String Contains a Number Value in Java (#14867)

Co-authored-by: Luis Javier Peris Morillo <javierperis@gmail.com>
This commit is contained in:
Azhwani 2023-10-26 08:18:01 +02:00 committed by GitHub
parent e23cb46bad
commit b4db616646
3 changed files with 217 additions and 71 deletions

View File

@ -1,72 +1,72 @@
<?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-operations-7</artifactId>
<name>core-java-string-operations-7</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>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${apache.commons.lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${commons-text.version}</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<apache.commons.lang3.version>3.12.0</apache.commons.lang3.version>
<commons-text.version>1.10.0</commons-text.version>
</properties>
<?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-operations-7</artifactId>
<name>core-java-string-operations-7</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>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${apache.commons.lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${commons-text.version}</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<apache.commons.lang3.version>3.13.0</apache.commons.lang3.version>
<commons-text.version>1.10.0</commons-text.version>
</properties>
</project>

View File

@ -0,0 +1,77 @@
package com.baeldung.strcontainsnumber;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import com.google.common.base.CharMatcher;
public class StrContainsNumberUtils {
static boolean checkUsingMatchesMethod(String input) {
if (input == null || input.isEmpty()) {
return false;
}
return input.matches(".*\\d.*");
}
static boolean checkUsingPatternClass(String input) {
if (input == null || input.isEmpty()) {
return false;
}
return Pattern.compile(".*\\d.*")
.matcher(input)
.matches();
}
static boolean checkUsingReplaceAllMethod(String input) {
if (input == null || input.isEmpty()) {
return false;
}
String result = input.replaceAll("\\d", "");
return result.length() != input.length();
}
static boolean checkUsingIsDigitMethod(String input) {
if (input == null || input.isEmpty()) {
return false;
}
for (char c : input.toCharArray()) {
if (Character.isDigit(c)) {
return true;
}
}
return false;
}
static boolean checkUsingStreamApi(String input) {
if (input == null || input.isEmpty()) {
return false;
}
return input.chars()
.anyMatch(Character::isDigit);
}
static boolean checkUsingApacheCommonsLang(String input) {
String result = StringUtils.getDigits(input);
return result != null && !result.isEmpty();
}
static boolean checkUsingGuava(String input) {
if (input == null || input.isEmpty()) {
return false;
}
String result = CharMatcher.forPredicate(Character::isDigit)
.retainFrom(input);
return !result.isEmpty();
}
}

View File

@ -0,0 +1,69 @@
package com.baeldung.strcontainsnumber;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import org.junit.Test;
public class StrContainsNumberUtilsUnitTest {
private static final String INPUT_WITH_NUMBERS = "We hope 2024 will be great";
private static final String INPUT_WITHOUT_NUMBERS = "Hello world";
@Test
public void givenInputString_whenUsingMatchesMethod_ThenCheck() {
assertTrue(StrContainsNumberUtils.checkUsingMatchesMethod(INPUT_WITH_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingMatchesMethod(INPUT_WITHOUT_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingMatchesMethod(""));
assertFalse(StrContainsNumberUtils.checkUsingMatchesMethod(null));
}
@Test
public void givenInputString_whenUsingPatternClass_ThenCheck() {
assertTrue(StrContainsNumberUtils.checkUsingPatternClass(INPUT_WITH_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingPatternClass(INPUT_WITHOUT_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingPatternClass(""));
assertFalse(StrContainsNumberUtils.checkUsingPatternClass(null));
}
@Test
public void givenInputString_whenUsingReplaceAllMethod_ThenCheck() {
assertTrue(StrContainsNumberUtils.checkUsingReplaceAllMethod(INPUT_WITH_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingReplaceAllMethod(INPUT_WITHOUT_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingReplaceAllMethod(""));
assertFalse(StrContainsNumberUtils.checkUsingReplaceAllMethod(null));
}
@Test
public void givenInputString_whenUsingIsDigitMethod_ThenCheck() {
assertTrue(StrContainsNumberUtils.checkUsingIsDigitMethod(INPUT_WITH_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingIsDigitMethod(INPUT_WITHOUT_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingIsDigitMethod(""));
assertFalse(StrContainsNumberUtils.checkUsingIsDigitMethod(null));
}
@Test
public void givenInputString_whenUsingStreamApi_ThenCheck() {
assertTrue(StrContainsNumberUtils.checkUsingStreamApi(INPUT_WITH_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingStreamApi(INPUT_WITHOUT_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingStreamApi(""));
assertFalse(StrContainsNumberUtils.checkUsingStreamApi(null));
}
@Test
public void givenInputString_whenUsingApacheCommonsLang_ThenCheck() {
assertTrue(StrContainsNumberUtils.checkUsingApacheCommonsLang(INPUT_WITH_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingApacheCommonsLang(INPUT_WITHOUT_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingApacheCommonsLang(""));
assertFalse(StrContainsNumberUtils.checkUsingApacheCommonsLang(null));
}
@Test
public void givenInputString_whenUsingGuava_ThenCheck() {
assertTrue(StrContainsNumberUtils.checkUsingGuava(INPUT_WITH_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingGuava(INPUT_WITHOUT_NUMBERS));
assertFalse(StrContainsNumberUtils.checkUsingGuava(""));
assertFalse(StrContainsNumberUtils.checkUsingGuava(null));
}
}