In C#, how to check whether a string contains an integer?

后端 未结 8 1220
慢半拍i
慢半拍i 2020-12-28 12:43

I just want to know, whether a String variable contains a parsable positive integer value. I do NOT want to parse the value right now.

Currently I

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-28 13:39

    You could use char.IsDigit:

         bool isIntString = "your string".All(char.IsDigit)
    

    Will return true if the string is a number

        bool containsInt = "your string".Any(char.IsDigit)
    

    Will return true if the string contains a digit

提交回复
热议问题