C# 检查字符串只包含数字
如何检查字符串是否为数字。我正在验证手机号码代码,其中应该有10位数字,并且只有数字格式C# 检查字符串只包含数字,c#,C#,如何检查字符串是否为数字。我正在验证手机号码代码,其中应该有10位数字,并且只有数字格式 string str="9848768447" if(str.Length==10 && Here I need condition to check string is number or not) { //Code goes here } 我是编程新手。请帮助我使用: 另一种使用字符的方式是数字: if(str.Length==10 && str.All(Char.
string str="9848768447"
if(str.Length==10 && Here I need condition to check string is number or not)
{
//Code goes here
}
我是编程新手。请帮助我使用:
另一种使用字符的方式是数字:
if(str.Length==10 && str.All(Char.IsDigit))
{
}
“它与unicode数字有问题”为什么还要显示它呢?D:你也可以使用regex
regex.IsMatch(input,@“^\D+$”
或regex.IsMatch(input,@“\D”)
解决方案不是更有效吗?@EaterOfCode:因为它很有用,是有效的,是对OP问题的直接回答。顺便说一句,它与带有数字说明符的正则表达式类似,但效率更高,可读性更强。@TimSchmelter不是真的,很明显(imo)他说的是拉丁数字,而不是阿拉伯数字或其他奇怪的数字字符。但是,嘿,不管是什么让你的船漂浮,或者从这个角度来看是整数。看看这里
if(str.Length==10 && str.All(Char.IsDigit))
{
}