Using system;
Using system. Collections. Generic;
Using system. text;
Using system. diagnostics;
Using system. Text. regularexpressions;
Namespace isnumbertest
{
/// <Summary>
/// Comparison of several methods to verify whether all strings are numeric
/// </Summary>
Class Program
{
Static void main (string [] ARGs)
{
Stopwatch SW; // Test Run Time
Int times = 50000; // number of cycles
String teststr = "12a"; // string used for testing
# 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
{
// Non-numeric
}
}
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;
}
}
}
The above is a test class
When the test string is "12a", the three running results are as follows:
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
When the test string is "123", the three running results are as follows:
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. Regular Expression advantages: it can verify the string format, such as a positive integer, with only two decimal places, whether it is a mobile phone number, and so on. This is not feasible in other methods, and when the demand changes, you only need to modify the regular expression. Disadvantages: the efficiency is not the highest. You need to have a certain understanding of the regular expression. 2. VB. net isnumeric method advantages: it is a ready-made method, easy to use, and the parameter is object, not limited to the disadvantages of string: you can only determine whether the given parameter is a numerical value (Boolean/byte/int6/INT/int6/single/double/decimal, for example, whether it is a positive integer. 3. Compared with other methods, the catch exception method is the most unavoidable method. When an exception is thrown, it consumes a lot of system resources. 4. Char. advantages of the isnumber method: the C # built-in method is easy to use and highly efficient. Disadvantages: You need to write your own method, and you can only determine whether all of them are numbers. Further judgment is not allowed. 5. Comparison of ASCII code advantages: High Efficiency disadvantages: You need to write your own method. You can only determine whether all the numbers are numbers. You can choose different methods based on your actual needs.