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

@ -65,7 +65,7 @@
<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>
<apache.commons.lang3.version>3.13.0</apache.commons.lang3.version>
<commons-text.version>1.10.0</commons-text.version>
</properties>

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