diff --git a/core-java-modules/core-java-string-operations-7/pom.xml b/core-java-modules/core-java-string-operations-7/pom.xml index 595f918fcd..a2c7036e34 100644 --- a/core-java-modules/core-java-string-operations-7/pom.xml +++ b/core-java-modules/core-java-string-operations-7/pom.xml @@ -1,72 +1,72 @@ - - - 4.0.0 - core-java-string-operations-7 - core-java-string-operations-7 - jar - - - com.baeldung.core-java-modules - core-java-modules - 0.0.1-SNAPSHOT - - - - - org.apache.commons - commons-lang3 - ${apache.commons.lang3.version} - - - org.apache.commons - commons-text - ${commons-text.version} - - - org.liquibase - liquibase-core - 4.9.1 - test - - - org.junit.jupiter - junit-jupiter - 5.8.1 - test - - - org.liquibase - liquibase-core - 4.9.1 - test - - - junit - junit - 4.13.2 - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - ${maven.compiler.source} - ${maven.compiler.target} - - - - - - - 11 - 11 - 3.12.0 - 1.10.0 - - + + + 4.0.0 + core-java-string-operations-7 + core-java-string-operations-7 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + + + org.apache.commons + commons-lang3 + ${apache.commons.lang3.version} + + + org.apache.commons + commons-text + ${commons-text.version} + + + org.liquibase + liquibase-core + 4.9.1 + test + + + org.junit.jupiter + junit-jupiter + 5.8.1 + test + + + org.liquibase + liquibase-core + 4.9.1 + test + + + junit + junit + 4.13.2 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + + + 11 + 11 + 3.13.0 + 1.10.0 + + \ No newline at end of file diff --git a/core-java-modules/core-java-string-operations-7/src/main/java/com/baeldung/strcontainsnumber/StrContainsNumberUtils.java b/core-java-modules/core-java-string-operations-7/src/main/java/com/baeldung/strcontainsnumber/StrContainsNumberUtils.java new file mode 100644 index 0000000000..a609b94cb8 --- /dev/null +++ b/core-java-modules/core-java-string-operations-7/src/main/java/com/baeldung/strcontainsnumber/StrContainsNumberUtils.java @@ -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(); + } + +} diff --git a/core-java-modules/core-java-string-operations-7/src/test/java/com/baeldung/strcontainsnumber/StrContainsNumberUtilsUnitTest.java b/core-java-modules/core-java-string-operations-7/src/test/java/com/baeldung/strcontainsnumber/StrContainsNumberUtilsUnitTest.java new file mode 100644 index 0000000000..9a60a827e0 --- /dev/null +++ b/core-java-modules/core-java-string-operations-7/src/test/java/com/baeldung/strcontainsnumber/StrContainsNumberUtilsUnitTest.java @@ -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)); + } + +}