检查字符串仅包含数字

use*_*835 -3 c#

如何检查字符串是否为数字.我正在验证手机号码,它应该有10位数字,并且只能采用数字格式.

string str="9848768447"
if(str.Length==10 &&  Here I need condition to check string is number or not)
{
 //Code goes here
}
Run Code Online (Sandbox Code Playgroud)

我是编程新手.请帮我

Tim*_*ter 6

用途int.TryParse:

int i;
if(str.Length==10 && int.TryParse(str, out i))
{
     //Code goes here
}
Run Code Online (Sandbox Code Playgroud)

另一种与unicode数字有关的方法是使用Char.IsDigit:

if(str.Length==10 && str.All(Char.IsDigit))
{

}
Run Code Online (Sandbox Code Playgroud)

  • "这有unicode数字的问题"为什么甚至显示它然后D: (2认同)