Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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));
}

}
Original file line number Diff line number Diff line change
@@ -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));
}

}