.net你怎么判断字符串是否全是数字

sohighthesky 2010-01-31 12:43:11
加精
今天在写代码时突然想起测试经常用Microsoft.VisualBasic.Information.IsNumeric判断 url参数是否为数字时的这个方法的效率

因为数字是字符串是直接使用的,所以不需要转型,也就没有用tryparse

结果一测试吓一跳,这个方法的效率是如此的低,再测试了下tryparse还不错,正则的也比较差,

没什么技术含量,看结果吧:



先拓展下字符串:



code
  public static class Common
{
//isDigit
public static bool isNumberic1(this string _string)
{
if (string.IsNullOrEmpty(_string))
return false;
foreach (char c in _string)
{
if (!char.IsDigit(c))
return false;
}
return true;
}

//vb isnumberic
public static bool isNumberic2(this string _string)
{
return !string.IsNullOrEmpty(_string) && Microsoft.VisualBasic.Information.IsNumeric(_string);
}
//try parese
public static bool isNumberic3(this string _string)
{
if (string.IsNullOrEmpty(_string))
return false;
int i = 0;
return int.TryParse(_string, out i);
}

//try catch
public static bool isNumberic4(this string _string)
{
if (string.IsNullOrEmpty(_string))
return false;
int i = 0;
try { int.Parse(_string); }
catch { return false; }
return true;
}
//regex
public static bool isNumberic5(this string _string)
{
return !string.IsNullOrEmpty(_string) && Regex.IsMatch(_string, "^\\d+$");
}
}


测试的代码:



code
    class Program
{
static void Main(string[] args)
{

Test("1234");
Test("1234a");
Test("a1234");
Test("");
Test(null);

}

static void Test(string str)
{
bool res1 = false, res2 = false, res3 = false, res4 = false, res5 = false;
Stopwatch wat = new Stopwatch();
wat.Start();
for (int i = 1; i < 100000; i++)
{
res1 = str.isNumberic1();
}
wat.Stop();
Console.WriteLine("isDigit {0}:{1},{2}", str, wat.ElapsedMilliseconds, res1);

wat.Reset();
wat.Start();
for (int i = 1; i < 100000; i++)
{
res2= str.isNumberic2();
}
wat.Stop();
Console.WriteLine("isNumberic {0}:{1},{2}", str, wat.ElapsedMilliseconds, res2);

wat.Reset();
wat.Start();
for (int i = 1; i < 100000; i++)
{
res3 = str.isNumberic3();
}
wat.Stop();
Console.WriteLine("try parse {0}:{1},{2}", str, wat.ElapsedMilliseconds, res3);

wat.Reset();
wat.Start();
for (int i = 1; i < 100000; i++)
{
res4 = str.isNumberic4();
}
wat.Stop();
Console.WriteLine("try catch {0}:{1},{2}", str, wat.ElapsedMilliseconds, res4);

wat.Reset();
wat.Start();
for (int i = 1; i < 100000; i++)
{
res5 = str.isNumberic5();
}
wat.Stop();
Console.WriteLine("regex {0}:{1},{2}", str, wat.ElapsedMilliseconds, res5);
Console.WriteLine();
}
}


下面是我本机的测试结果



isDigit 1234:0,True
isNumberic 1234:173,True
try parse 1234:21,True
try catch 1234:23,True
regex 1234:138,True

isDigit 1234a:0,False
isNumberic 1234a:204,False
try parse 1234a:20,False
try catch 1234a:5309,False
regex 1234a:151,False

isDigit a1234:0,False
isNumberic a1234:191,False
try parse a1234:16,False
try catch a1234:5229,False
regex a1234:109,False

isDigit :0,False
isNumberic :0,False
try parse :0,False
try catch :1,False
regex :0,False

isDigit :0,False
isNumberic :1,False
try parse :0,False
try catch :1,False
regex :0,False

结果:循环判断是否是数字字符效率是最高的

而vbscript的方法效率比较低了

顺便测试了下vbscript里的left和right方法效率也一样的低,还不及substring的十分之一

所以vbscript里虽然有几个方法从名字看来比较好用,实际却比较杯具。
...全文
6390 263 打赏 收藏 转发到动态 举报
写回复
用AI写文章
263 条回复
切换为时间正序
请发表友善的回复…
发表回复
weilian20101 2012-05-15
  • 打赏
  • 举报
回复
感谢分享
zhaoguoxin63 2010-08-19
  • 打赏
  • 举报
回复
这种内容,我知道的太少了,应该好好学习。
nankec 2010-06-25
  • 打赏
  • 举报
回复
char
很经典
xf198903 2010-06-11
  • 打赏
  • 举报
回复
csdn好像用缓存的,我改了头像现在都没有变过来,不知道是不是这个样子的。。。。。
xf198903 2010-06-11
  • 打赏
  • 举报
回复
不用try catch.......
zhubo006 2010-05-25
  • 打赏
  • 举报
回复
扣得真细啊,楼主
sunwei_158328 2010-02-20
  • 打赏
  • 举报
回复
学习中 谢谢分享!!!!!
gsz_stylm 2010-02-08
  • 打赏
  • 举报
回复
[Quote=引用 100 楼 jenny0810 的回复:]
看来try..catch还是少用
[/Quote]
同感
mslagee 2010-02-08
  • 打赏
  • 举报
回复
谢谢分享!!!
cchvsgame 2010-02-08
  • 打赏
  • 举报
回复
不太考虑效率的时候,还是用吧
Jelly_tracy 2010-02-08
  • 打赏
  • 举报
回复
up
zhuoyue 2010-02-08
  • 打赏
  • 举报
回复
mark
段传涛 2010-02-07
  • 打赏
  • 举报
回复
pack away this artical
ncuhao 2010-02-07
  • 打赏
  • 举报
回复
很受用.
HolyPlace 2010-02-07
  • 打赏
  • 举报
回复
就喜欢这种东西
pjhuanghao 2010-02-07
  • 打赏
  • 举报
回复
pkyinjianjun 2010-02-07
  • 打赏
  • 举报
回复
日日日日日日日日
pkyinjianjun 2010-02-07
  • 打赏
  • 举报
回复
山山山山山山山山山山山山
pkyinjianjun 2010-02-07
  • 打赏
  • 举报
回复
水水水水水水水水水水水水草莽漢英勇士兵營壘球鞋匠心切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切切
doctorlee 2010-02-06
  • 打赏
  • 举报
回复
一直想知道这个,谢谢楼主~
加载更多回复(236)

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧