判斷一個字符串是否全是數字的多種方法及其性能比較(C#實現)
2024-07-21 02:18:28
供稿:網友
在編程的時候,經常遇到要判斷一個字符串中的字符是否全部是數字(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秒!
結果全部出來了,該怎樣選擇大家心里肯定已經有數了,我也不需要再說什么了。
祝大家新年愉快!