How do I find out if first character of a string is a number?

前端 未结 6 1170
暖寄归人
暖寄归人 2020-11-29 18:13

In Java is there a way to find out if first character of a string is a number?

One way is

string.startsWith(\"1\")

and do the abov

6条回答
  •  一向
    一向 (楼主)
    2020-11-29 18:51

    Regular expressions are very strong but expensive tool. It is valid to use them for checking if the first character is a digit but it is not so elegant :) I prefer this way:

    public boolean isLeadingDigit(final String value){
        final char c = value.charAt(0);
        return (c >= '0' && c <= '9');
    }
    

提交回复
热议问题