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

前端 未结 6 1166
暖寄归人
暖寄归人 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
    Character.isDigit(string.charAt(0))
    

    Note that this will allow any Unicode digit, not just 0-9. You might prefer:

    char c = string.charAt(0);
    isDigit = (c >= '0' && c <= '9');
    

    Or the slower regex solutions:

    s.substring(0, 1).matches("\\d")
    // or the equivalent
    s.substring(0, 1).matches("[0-9]")
    

    However, with any of these methods, you must first be sure that the string isn't empty. If it is, charAt(0) and substring(0, 1) will throw a StringIndexOutOfBoundsException. startsWith does not have this problem.

    To make the entire condition one line and avoid length checks, you can alter the regexes to the following:

    s.matches("\\d.*")
    // or the equivalent
    s.matches("[0-9].*")
    

    If the condition does not appear in a tight loop in your program, the small performance hit for using regular expressions is not likely to be noticeable.

    0 讨论(0)
  • 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');
    }
    
    0 讨论(0)
  • 2020-11-29 19:02
    regular expression starts with number->'^[0-9]' 
    Pattern pattern = Pattern.compile('^[0-9]');
     Matcher matcher = pattern.matcher(String);
    
    if(matcher.find()){
    
    System.out.println("true");
    }
    
    0 讨论(0)
  • 2020-11-29 19:03

    I just came across this question and thought on contributing with a solution that does not use regex.

    In my case I use a helper method:

    public boolean notNumber(String input){
        boolean notNumber = false;
        try {
            // must not start with a number
            @SuppressWarnings("unused")
            double checker = Double.valueOf(input.substring(0,1));
        }
        catch (Exception e) {
            notNumber = true;           
        }
        return notNumber;
    }
    

    Probably an overkill, but I try to avoid regex whenever I can.

    0 讨论(0)
  • 2020-11-29 19:08

    IN KOTLIN :

    Suppose that you have a String like this :

    private val phoneNumber="9121111111"
    

    At first you should get the first one :

    val firstChar=phoneNumber.slice(0..0)
    

    At second you can check the first char that return a Boolean :

    firstChar.isInt() // or isFloat()
    
    0 讨论(0)
  • 2020-11-29 19:08

    To verify only first letter is number or character -- For number Character.isDigit(str.charAt(0)) --return true

    For character Character.isLetter(str.charAt(0)) --return true

    0 讨论(0)
提交回复
热议问题