C # How do you determine whether a string is a number?

Source: Internet
Author: User
Tags try catch
Today, when I write code, I suddenly think of the efficiency of this method when I often use Microsoft. VisualBasic. Information. isnumeric to determine whether the URL parameter is a number.
Because numbers are directly used as strings, tryparse is not used without transformation.
As a result, the efficiency of this method is so low. The tryparse is good and the regular expression is poor,
There is no technical content. Check the results:

Expand the following string first:

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) // If (C <'0' | C> '9') // The best method. Add 0 to the test data below, the efficiency of this method is about 10 milliseconds.
Return false;
}
Return true;
}

// VB isnumberic
Public static bool isnumberic2 (this string _ string)
{
Return! String. isnullorempty (_ string) & Microsoft. VisualBasic. Information. isnumeric (_ string );
}
// Try parse
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;
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 + $ ");
}
}

 

Test code:

 

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 ();
}
}

 

The following is the test result of my local machine.

 

Isdiit 1234: 5, true
Isnumberic 1234: 166, true
Try parse 1234: 21, true
Try catch 1234: 22, true
RegEx 1234: 134, true

Isdigit 1234a: 5, false
Isnumberic 1234a: 196, false
Try parse 1234a: 19, false
Try catch 1234a: 5182, false
RegEx 1234a: 150, false

Isdigit a1234: 2, false
Isnumberic a1234: 184, false
Try parse a1234: 16, false
Try catch a1234: 5084, false
RegEx a1234: 106, false

Isdigit: 1, false
Isnumberic: 0, false
Try parse: 0, false
Try catch: 1, false
RegEx: 0, false

Isdigit: 1, false
Isnumberic: 0, false
Try parse: 1, false
Try catch: 1, false
RegEx: 0, false

 

Result: the efficiency of determining whether it is a numeric character in a loop is the highest.
However, the Visual Basic method is less efficient.
By the way, we tested that the Left and Right methods in Visual Basic are equally less efficient than the substring's tenth.
Therefore, although there are several methods in Visual Basic that seem to be easy to use from the perspective of name, they are actually quite handy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.