How to check a string starts with numeric number?

前端 未结 6 2024
野趣味
野趣味 2021-02-06 20:44

I have a string which contains alphanumeric character.

I need to check whether the string is started with number.

Thanks,

6条回答
  •  清酒与你
    2021-02-06 21:07

    Sorry I didn't see your Java tag, was reading question only. I'll leave my other answers here anyway since I've typed them out.

    Java

    String myString = "9Hello World!";
    if ( Character.isDigit(myString.charAt(0)) )
    {
        System.out.println("String begins with a digit");
    }
    

    C++:

    string myString = "2Hello World!";
    
    if (isdigit( myString[0]) )
    {
        printf("String begins with a digit");
    }
    

    Regular expression:

    \b[0-9]
    

    Some proof my regex works: Unless my test data is wrong?

提交回复
热议问题