using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace IsNumberTest
{
/// <summary>
/// 幾種驗證字串是否全為數字方法的比較
/// </summary>
class Program
{
static void Main(string[] args)
{
Stopwatch sw; //測試回合時間
int times = 50000; //迴圈次數
string testStr = "12a"; //測試用字串
#region Regular Expression Method
sw = Stopwatch.StartNew();
Regex regIsNum = new Regex (@"^/d+$");
for (int i = 0; i < times; i++)
{
regIsNum.IsMatch(testStr);
}
sw.Stop();
Console.WriteLine("Regular Expression Method Last Time: " + sw.Elapsed.Milliseconds + " ms");
#endregion
#region VB.NET IsNumeric Method
sw = Stopwatch.StartNew();
for (int i = 0; i < times; i++)
{
Microsoft.VisualBasic.Information.IsNumeric(testStr);
}
sw.Stop();
Console.WriteLine("VB.NET IsNumeric Method Last Time: " + sw.Elapsed.Milliseconds + " ms");
#endregion
#region Customer Method
sw = Stopwatch.StartNew();
for (int i = 0; i < times; i++)
{
IsNum(testStr);
}
sw.Stop();
Console.WriteLine("Customer Method Last Time: " + sw.Elapsed.Milliseconds + " ms");
#endregion
#region Improved Customer Method
sw = Stopwatch.StartNew();
for (int i = 0; i < times; i++)
{
ImpIsNum(testStr);
}
sw.Stop();
Console.WriteLine("Improved Customer Method Last Time: " + sw.Elapsed.Milliseconds + " ms");
#endregion
#region Catch Exception Method
sw = Stopwatch.StartNew();
for (int i = 0; i < times; i++)
{
try
{
int.Parse(testStr);
}
catch
{
//非數字
}
}
sw.Stop();
Console.WriteLine("Catch Exception Method Last Time: " + sw.Elapsed.Milliseconds + " ms");
#endregion
Console.ReadLine();
}
public static bool IsNum(String srcStr)
{
for (int i = 0; i < srcStr.Length; i++)
{
if (!Char.IsNumber(srcStr, i))
return false;
}
return true;
}
public static bool ImpIsNum(String srcStr)
{
for (int i = 0; i < srcStr.Length; i++)
{
if (srcStr[i] <= '0' || srcStr[i] >= '9')
return false;
}
return true;
}
}
}
以上為一個測試類別
當測試字串為"12a"時,三次運行結果如下:
Regular Expression Method Last Time: 58 ms
VB.NET IsNumeric Method Last Time: 149 ms
Customer Method Last Time: 3 ms
Improved Customer Method Last Time: 2 ms
Catch Exception Method Last Time: 769 ms
Regular Expression Method Last Time: 37 ms
VB.NET IsNumeric Method Last Time: 123 ms
Customer Method Last Time: 3 ms
Improved Customer Method Last Time: 2 ms
Catch Exception Method Last Time: 798 ms
Regular Expression Method Last Time: 43 ms
VB.NET IsNumeric Method Last Time: 126 ms
Customer Method Last Time: 3 ms
Improved Customer Method Last Time: 2 ms
Catch Exception Method Last Time: 910 ms
當測試字串為"123"時,三次運行結果如下:
Regular Expression Method Last Time: 34 ms
VB.NET IsNumeric Method Last Time: 124 ms
Customer Method Last Time: 3 ms
Improved Customer Method Last Time: 2 ms
Catch Exception Method Last Time: 12 ms
Regular Expression Method Last Time: 32 ms
VB.NET IsNumeric Method Last Time: 131 ms
Customer Method Last Time: 3 ms
Improved Customer Method Last Time: 2 ms
Catch Exception Method Last Time: 12 ms
Regular Expression Method Last Time: 32 ms
VB.NET IsNumeric Method Last Time: 123 ms
Customer Method Last Time: 3 ms
Improved Customer Method Last Time: 2 ms
Catch Exception Method Last Time: 12 ms
1、Regex 優點:可以驗證字串的格式,比如為正整數、只帶2位小數、是否為手機號碼等,這是其它方法做不到的,而且需求改變時,只需要修改一下Regex就可以了 缺點:效率不是最高的,需要對Regex有一定程度的瞭解 2、VB.NET IsNumeric方法 優點:是現成的方法,用起來方便,且參數為object,並不局限於string 缺點:只能判斷所給的參數是否是數值(boolean/byte/int6/int/int6/single/double/decimal),無法作進一步的判斷,比如是否為正整數 3、Catch Exception 方法 相對於其它方法而言,這是最應該避免使用的一種方法,在有Exception拋出的時候,消耗大量系統資源 4、Char.IsNumber 方法 優點:C#內建的方法,用起來方便,效率高 缺點:需要自己寫方法,同樣只能判斷是否全為數字,無法作進一步判斷 5、比較ASCII碼 優點:效率高 缺點:需要自己寫方法,同樣只能判斷是否全為數字,無法作進一步判斷 可以根據實際需求,選用不同的方法。