Check a character in a String is number or not in Java

All you need is this simple function in java.lang package.

Character.isDigit(c)

Of course, if want to check a character in a string, need to get the char first by using the following.

str.charAt(i)
Category >> Basics  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>

  1. Abdul Shameer on 2016-1-5

    It could be easy to do as follows:


    public boolean isStringDigit(String str){
    try{
    Integer.valueOf(str);
    }catch(NumberFormatException ne){
    System.out.println("In NFE ");return false;
    }
    return true;
    }

  2. Apurv Dubey on 2017-4-15

    this is about character in a string and not whole string. For ex: “`Character.isDigit(“hello1world”.charAt(5))“` returns true

Leave a comment

*