方法一(字符串處理):
public static bool IsNum(string str)
{
bool blResult = true;//默認狀態下是數字
if(str == "")
blResult = false;
else
{
foreach(char Char in str)
{
if(!char.IsNumber(Char))
{
blResult = false;
break;
}
}
if(blResult)
{
if(int.Parse(str) == 0)
blResult = false;
}
}
return blResult;
}
方法二(正則表達式):
static bool IsNumeric(string str)
{
System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[0-9]\d*$");
return reg1.IsMatch(str);
}