static bool IsNumeric(string str)
{
if (str==null || str.Length==0)
return false;
foreach(char c in str)
{
if (!Char.IsNumber(c))
{
return false;
}
}
return true;
}
正则表达的写法是:
static bool IsNumeric(string str)
{
System.Text.RegularExpressions.Regex reg1
= new System.Text.RegularExpressions.Regex(@"^[-]?/d+[.]?/d*$");
return reg1.IsMatch(str);
}
{
if (str==null || str.Length==0)
return false;
foreach(char c in str)
{
if (!Char.IsNumber(c))
{
return false;
}
}
return true;
}
正则表达的写法是:
{
System.Text.RegularExpressions.Regex reg1
= new System.Text.RegularExpressions.Regex(@"^[-]?/d+[.]?/d*$");
return reg1.IsMatch(str);
}
本文介绍了两种检查字符串是否仅包含数字的有效方法。第一种方法通过遍历字符串中的每个字符并使用Char.IsNumber()来判断;第二种方法利用正则表达式进行匹配,提供了一种简洁的实现方式。
210

被折叠的 条评论
为什么被折叠?



