Fastest way to check if string contains only digits

后端 未结 20 834
挽巷
挽巷 2020-12-04 09:07

I know a few ways how to check this. regex,int.parse,tryparse,looping.

can anyone tell me what is the fastest way to check?

the nee

20条回答
  •  离开以前
    2020-12-04 09:42

    Very Clever and easy way to detect your string is contains only digits or not is this way:

    string s = "12fg";
    
    if(s.All(char.IsDigit))
    {
       return true; // contains only digits
    }
    else
    {
       return false; // contains not only digits
    }
    

提交回复
热议问题