How can I check if a string is a number?

后端 未结 25 1541
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 21:04

I\'d like to know on C# how to check if a string is a number (and just a number).

Example :

141241   Yes
232a23   No
12412a   No

an

相关标签:
25条回答
  • 2020-11-27 21:37

    Use Int32.TryParse()

    int num;
    
    bool isNum = Int32.TryParse("[string to test]", out num);
    
    if (isNum)
    {
        //Is a Number
    }
    else
    {
        //Not a number
    }
    

    MSDN Reference

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