How to Check if the String Contains only Digits

In this post, we will write a Java program to check if the String contains only Unicode digits.

We will see two different ways:
  1. Using Character.isDigit() method
  2. Using Regular Expressions

How to Check if the String Contains Only Digits

We are going to use the Character.isDigit() method to check if the character is a digit or not. 

Let's write a Java method to check if the given String contains only digits:
private static boolean isNumeric(final CharSequence cs) {
        if (isEmpty(cs)) {
            return false;
        }
        final int sz = cs.length();
        for (int i = 0; i < sz; i++) {
            if (!Character.isDigit(cs.charAt(i))) {
                return false;
            }
        }
        return true;
    }
Let's write a complete Java program with JUnit test cases to test the above method:
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

/**
 * How to Checks Checks if the String contains only digits
 * @author javaguides.net
 *
 */
public class IsNumiricExample {

    private static boolean isEmpty(final CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    private static boolean isNumeric(final CharSequence cs) {
        if (isEmpty(cs)) {
            return false;
        }
        final int sz = cs.length();
        for (int i = 0; i < sz; i++) {
            if (!Character.isDigit(cs.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    @Test
    public void testIsNumeric() {
        assertFalse(isNumeric(null));
        assertFalse(isNumeric(""));
        assertFalse(isNumeric(" "));
        assertFalse(isNumeric("a"));
        assertFalse(isNumeric("A"));
        assertFalse(isNumeric("kgKgKgKgkgkGkjkjlJlOKLgHdGdHgl"));
        assertFalse(isNumeric("ham kso"));
        assertTrue(isNumeric("1"));
        assertTrue(isNumeric("1000"));
        assertTrue(isNumeric("\u0967\u0968\u0969"));
        assertFalse(isNumeric("\u0967\u0968 \u0969"));
        assertFalse(isNumeric("2.3"));
        assertFalse(isNumeric("10 00"));
        assertFalse(isNumeric("hkHKHik6iUGHKJgU7tUJgKJGI87GIkug"));
        assertFalse(isNumeric("_"));
        assertFalse(isNumeric("hkHKHik*khbkuh"));
        assertFalse(isNumeric("+123"));
        assertFalse(isNumeric("-123"));
    }
}

Using Regular Expressions

Java's String class contains a method called matches(), which you can use with a regular expression to check the pattern of the string. To check if a string contains only digits, you can use the regular expression "\\d+". 

Here's how you can use it:
public class DigitCheck {
    public static void main(String[] args) {
        String str = "12345";
        if (str.matches("\\d+")) {
            System.out.println("The string contains only digits.");
        } else {
            System.out.println("The string contains characters other than digits.");
        }
    }
}
Output:
The string contains only digits.
The regular expression method is concise and often preferable when you need a quick check. However, for very long strings, the iterative method might be more performative as regular expressions have some overhead.

Related Java String Programs with Output

Comments