BAEL-6352: Check if the First Letter of a String is a Number (#13883)

This commit is contained in:
Azhwani 2023-04-29 00:13:35 +02:00 committed by GitHub
parent 3510d4eaa3
commit f1ac6f1857
2 changed files with 120 additions and 0 deletions

View File

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

View File

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