From f1ac6f18572c17f8f13bf3df95be34a82ac9ccb5 Mon Sep 17 00:00:00 2001 From: Azhwani <13301425+azhwani@users.noreply.github.com> Date: Sat, 29 Apr 2023 00:13:35 +0200 Subject: [PATCH] BAEL-6352: Check if the First Letter of a String is a Number (#13883) --- .../firstchardigit/FirstCharDigit.java | 62 +++++++++++++++++++ .../FirstCharDigitUnitTest.java | 58 +++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 core-java-modules/core-java-string-operations-5/src/main/java/com/baeldung/firstchardigit/FirstCharDigit.java create mode 100644 core-java-modules/core-java-string-operations-5/src/test/java/com/baeldung/firstchardigit/FirstCharDigitUnitTest.java diff --git a/core-java-modules/core-java-string-operations-5/src/main/java/com/baeldung/firstchardigit/FirstCharDigit.java b/core-java-modules/core-java-string-operations-5/src/main/java/com/baeldung/firstchardigit/FirstCharDigit.java new file mode 100644 index 0000000000..a43127af1a --- /dev/null +++ b/core-java-modules/core-java-string-operations-5/src/main/java/com/baeldung/firstchardigit/FirstCharDigit.java @@ -0,0 +1,62 @@ +package com.baeldung.firstchardigit; + +import java.util.regex.Pattern; + +import com.google.common.base.CharMatcher; + +public class FirstCharDigit { + + public static boolean checkUsingCharAtMethod(String str) { + if (str == null || str.length() == 0) { + return false; + } + + char c = str.charAt(0); + return c >= '0' && c <= '9'; + } + + public static boolean checkUsingIsDigitMethod(String str) { + if (str == null || str.length() == 0) { + return false; + } + + return Character.isDigit(str.charAt(0)); + } + + public static boolean checkUsingPatternClass(String str) { + if (str == null || str.length() == 0) { + return false; + } + + return Pattern.compile("^[0-9].*") + .matcher(str) + .matches(); + } + + public static boolean checkUsingMatchesMethod(String str) { + if (str == null || str.length() == 0) { + return false; + } + + return str.matches("^[0-9].*"); + } + + public static boolean checkUsingCharMatcherInRangeMethod(String str) { + if (str == null || str.length() == 0) { + return false; + } + + return CharMatcher.inRange('0', '9') + .matches(str.charAt(0)); + } + + public static boolean checkUsingCharMatcherForPredicateMethod(String str) { + if (str == null || str.length() == 0) { + return false; + } + + return CharMatcher.forPredicate(Character::isDigit) + .matches(str.charAt(0)); + } + +} diff --git a/core-java-modules/core-java-string-operations-5/src/test/java/com/baeldung/firstchardigit/FirstCharDigitUnitTest.java b/core-java-modules/core-java-string-operations-5/src/test/java/com/baeldung/firstchardigit/FirstCharDigitUnitTest.java new file mode 100644 index 0000000000..0095ebcaf3 --- /dev/null +++ b/core-java-modules/core-java-string-operations-5/src/test/java/com/baeldung/firstchardigit/FirstCharDigitUnitTest.java @@ -0,0 +1,58 @@ +package com.baeldung.firstchardigit; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +class FirstCharDigitUnitTest { + + @Test + void givenString_whenUsingCharAtMethod_thenSuccess() { + assertTrue(FirstCharDigit.checkUsingCharAtMethod("12 years")); + assertFalse(FirstCharDigit.checkUsingCharAtMethod("years")); + assertFalse(FirstCharDigit.checkUsingCharAtMethod("")); + assertFalse(FirstCharDigit.checkUsingCharAtMethod(null)); + } + + @Test + void givenString_whenUsingIsDigitMethod_thenSuccess() { + assertTrue(FirstCharDigit.checkUsingIsDigitMethod("10 cm")); + assertFalse(FirstCharDigit.checkUsingIsDigitMethod("cm")); + assertFalse(FirstCharDigit.checkUsingIsDigitMethod("")); + assertFalse(FirstCharDigit.checkUsingIsDigitMethod(null)); + } + + @Test + void givenString_whenUsingPatternClass_thenSuccess() { + assertTrue(FirstCharDigit.checkUsingPatternClass("1 kg")); + assertFalse(FirstCharDigit.checkUsingPatternClass("kg")); + assertFalse(FirstCharDigit.checkUsingPatternClass("")); + assertFalse(FirstCharDigit.checkUsingPatternClass(null)); + } + + @Test + void givenString_whenUsingMatchesMethod_thenSuccess() { + assertTrue(FirstCharDigit.checkUsingMatchesMethod("123")); + assertFalse(FirstCharDigit.checkUsingMatchesMethod("ABC")); + assertFalse(FirstCharDigit.checkUsingMatchesMethod("")); + assertFalse(FirstCharDigit.checkUsingMatchesMethod(null)); + } + + @Test + void givenString_whenUsingCharMatcherInRangeMethod_thenSuccess() { + assertTrue(FirstCharDigit.checkUsingCharMatcherInRangeMethod("2023")); + assertFalse(FirstCharDigit.checkUsingCharMatcherInRangeMethod("abc")); + assertFalse(FirstCharDigit.checkUsingCharMatcherInRangeMethod("")); + assertFalse(FirstCharDigit.checkUsingCharMatcherInRangeMethod(null)); + } + + @Test + void givenString_whenUsingCharMatcherForPredicateMethod_thenSuccess() { + assertTrue(FirstCharDigit.checkUsingCharMatcherForPredicateMethod("100")); + assertFalse(FirstCharDigit.checkUsingCharMatcherForPredicateMethod("abdo")); + assertFalse(FirstCharDigit.checkUsingCharMatcherForPredicateMethod("")); + assertFalse(FirstCharDigit.checkUsingCharMatcherForPredicateMethod(null)); + } + +}