检查字符串仅包含数字

时间:2014-06-24 08:36:56

标签: c#

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

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

我是编程新手。请帮帮我

1 个答案:

答案 0 :(得分:6)

使用int.TryParse

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

has issues with unicode digits使用Char.IsDigit的另一种方式:

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

}
相关问题