How to check if my string only numeric

后端 未结 7 2056
臣服心动
臣服心动 2020-12-18 17:49

How I can check if my string only contain numbers?

I don\'t remember. Something like isnumeric?

7条回答
  •  既然无缘
    2020-12-18 18:35

    Your question is not clear. Is . allowed in the string? Is ¼ allowed?

    string source = GetTheString();
    
    //only 0-9 allowed in the string, which almost equals to int.TryParse
    bool allDigits = source.All(char.IsDigit); 
    bool alternative = int.TryParse(source,out result);
    
    //allow other "numbers" like ¼
    bool allNumbers = source.All(char.IsNumber);
    

提交回复
热议问题