`

C#验证输入的是否数字的方法

阅读更多

C#验证输入的是否数字的方法
其实用正则表达式也可以
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);
}

判断一个字符串是否全是数字的多种方法及其性能比较(C#实现)

在编程的时候,经常遇到要判断一个字符串中的字符是否全部是数字(0-9),本来是一个很容易实现的功能,但程序员首先会想到的是,这样简单的功能有没有现成的函数可以用呢?VB.NET中有个IsNumeric(object),C#中只有判断单个字符的Char.IsNumber(),IsNumeric可以判断double类型的数字字符串,但无法排除正负号和小数点,如果判断字符串是否是一个数的话用它挺合适,但不能用来判断字符串是否全部由数字组成的。没现成的方法了,只好自己写函数:

public static bool IsNum(String str)
{
for(int i=0;i<str.Length;i++)
{
if(!Char.IsNumber(str,i))
return false;
}
return true;
}

或用正则表达式:"^\d+$"

还可以用Int32.Parse()抛出的Exception来判断:

try
{
Int32.Parse(toBeTested);
}
catch
{
//发生了异常,那么就不是数字了。
}

那么哪一种方法最好呢?各有优劣。我顺手写了一个程序对每一种方法所需要的时间进行了测试。测试程序Main()内容如下:

Regex isNumeric = new Regex(@"^\d+$");
int times = 10000000;
int start, end;
int i;
string toBeTested = "6741s";

#region Test user function
start = System.Environment.TickCount;
for(i=0; i<times; i++)
{
TimingTest.IsNum(toBeTested);
}
end = System.Environment.TickCount;
Console.WriteLine("User function Time: " + (end-start)/1000.0 + " Seconds");
#endregion

#region Test Regular Expression
start = System.Environment.TickCount;
for(i=0; i<times; i++)
{
isNumeric.IsMatch(toBeTested);
}
end = System.Environment.TickCount;
Console.WriteLine("Regular Expression Time: " + (end-start)/1000.0 + " Seconds");
#endregion

#region Test Exception
start = System.Environment.TickCount;
for(i=0; i<times/100; i++)
{
try
{
Int32.Parse(toBeTested);
}
catch
{
//发生了异常,那么就不是数字了。
}

}
end = System.Environment.TickCount;
Console.WriteLine("Exception Time: " + (end-start)/10.0 + " Seconds");
#endregion

#region Test VB.NET IsNumeric()
start = System.Environment.TickCount;
for(i=0; i<times/10; i++)
{
Microsoft.VisualBasic.Information.IsNumeric(toBeTested);
}
end = System.Environment.TickCount;
Console.WriteLine("VB.NET IsNumeric() Time: " + (end-start)/100.0 + " Seconds");
#endregion


因为Exception所用的时间太长,所以只测试了1/100,这样不太严谨,但是数量级不会错的。

三次运行的结果是:
User function Time:1.938 Seconds
Regular Expression Time: 11.921 Seconds
Exception Time: 600 Seconds
VB.NET IsNumeric() Time: 40.797 Seconds
User function Time:1.953 Seconds
Regular Expression Time: 12.016 Seconds
Exception Time: 590.6 Seconds
VB.NET IsNumeric() Time: 40 Seconds
User function Time:2.000 Seconds
Regular Expression Time: 12 Seconds
Exception Time: 595.3 Seconds
VB.NET IsNumeric() Time: 39.69 Seconds
平均时间:

1.964
11.979
595.3
40.162

速度之比依次约为:303 : 49.7 : 1 : 14.82

结果很明显,自定义函数速度最快,异常的速度最慢。假如不需要抛异常的话string toBeTested = "67412";结果就成了:

User function Time: 1.922 Seconds
Regular Expression Time: 9.64 Seconds
Exception Time: 3.1 Seconds
VB.NET IsNumeric() Time: 39.07 Seconds
速度之比依次约为:20.33 : 4.05 : 12.60: 1

结论:

自定义函数可以获得最大的灵活性和最高的性能,而且复杂性也不高,是最佳的方法。
正则表达式法和IsNumeric速度在同一个数量级上,但正则表达式可以确定一种字符串的格式,比如规定一定要有或没有小数点等,而IsNumeric无法做到。
使用异常是应该避免的。建议仅把Exception作为处理异常的一种手段,而不是作为控制流程的一种手段。测试也表明,当有异常抛出时,要消耗大量的资源。
IsNumeric是现成的函数,用起来最省事,只能判断所给的参数是否是数值(boolean/byte/int16/int32/int64/single/double/decimal),无法作进一步的要求(是否有小数点等)。但IsNumeric的参数是object,不局限于string。

写到这里,我不禁想,还有没有比自定义函数更快的方法呢?答案是肯定的。在前面的自定义函数中,用的是Char.IsNumber()函数,这个函数不仅能判断标准ASCII码中的'1',甚至对全角中文的'1'的判断也是true,可见Char.IsNumber()判断的是所有的Unicode字符中的数字,其他语言中的数字也包括了。假如我们只允许ASCII中的'1'的话,我们可以这样改:

public static bool IsNum(String str)
{
for(int i=0;i<str.Length;i++)
{
if(str[i]<'0' || str[i]>'9')
return false;
}
return true;
}

测试结果也令我吃惊,这样比原来的IsNum速度提高了近10倍,平均执行时间是0.205秒!


在Textbox的輸入中﹐我們常常需要控制輸入的類型﹐比如說只能輸入數字﹐當然實現的方法很多﹐我總結了一下我做過的一些項目﹐我常會使用以下這三種﹕
1﹑使用Try...Catch
privatestaticboolIsNumeric(stringitemValue,intintFLag)
{
try
{
inti=Convert.ToInt32(itemValue);
returntrue;
}

catch
{
returnfalse;
}

}
2﹑使用正則表達式
usingSystem.Text.RegularExpressions;

privatestaticboolIsNumeric(stringitemValue)
{
return(IsRegEx("^(-?[0-9]*[.]*[0-9]{0,3})$",itemValue));
}


privatestaticboolIsRegEx(stringregExValue,stringitemValue)
{
try
{
Regexregex
=newSystem.Text.RegularExpressions.Regex(regExValue);
if(regex.IsMatch(itemValue))returntrue;
elsereturnfalse;
}

catch(Exception)
{
returnfalse;
}

finally
{
}

}


3﹑判斷輸入的keyCode
publicstaticboolIsNumeric(System.Windows.Forms.KeyPressEventArgse)
{
if((e.KeyChar>=(char)48&&e.KeyChar<=(char)57)||
e.KeyChar
==(char)8||e.KeyChar==(char)45||e.KeyChar==(char)47)
{
}

else
{
e.Handled
=true;
}

returntrue;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics