C # determines whether it is a number

Source: Internet
Author: User

Someone suggested using the int. Parse (string) method, and then capture exceptions to determine the returned value. A better way is to use a regular expression:
Public int IsNumeric (string str)
{
Int I;
If (str! = Null & Regex. IsMatch (str, @ "^ d + $ "))
I = int. Parse (str );
Else
I =-1;
Return I;
}

This should be a good solution. However, if you want to put forward higher requirements, such as not only determining whether the string is all numbers, but also asking for conversion after the judgment. The above function is defective because the '-' symbol cannot be determined in the above regular expression. Therefore, the preceding function can only convert non-negative values. What's more, not only integers, but also floating-point numbers, especially scientific notation strings, such as: string s = "-3.14159E + 10"

The E,., and + characters are special, and they are also required to appear in the position of a floating point number or scientific counting method. So you cannot use regular expressions. At least it is very troublesome (I think so, maybe there is a good way ). The problem is returned to the original solution, that is, using the Parse () method and capturing exceptions. The following describes how to implement this function:
Public bool IsNumeric (string s, out double result)
{
Bool bReturn = true;
Try
{
Result = double. Parse (s );
}
Catch
{
Result = 0;
BReturn = false;
}
Return bReturn;
}
For example, call:

String s1 = "abc ";
String s2 = "-3.14159E + 10 ";
Double d1, d2;
Bool ty1 = IsNumeric (s1, out d1 );
Bool ty2 = IsNumeric (s2, out d2 );
Result:
Ty1 = false; d1 = 0;
Ty2 = true; d2 = 31415900000.

Note: Because floating point numbers are involved,-1 cannot be used as the return value of an error, as shown in the previous example. That is to say, this method has two return values: one is to judge whether the conversion is successful, and the other is the floating point value after the conversion is successful. I use out to solve this problem.

This method is still applicable to integer types. You only need to forcibly convert the returned values.

In VB, there is an inumeric function to judge
In C #. net, there is no more.

Many try {} is used on the Internet. This method does not work. However, because it is implemented by catch errors, the data will be slow (I have tried it, so the following method uses a regular expression.

        // Determine whether it is a positive integer
        Public static bool IsInt (string inString)
        {
                Regex regex = new Regex ("^ [0-9] * [1-9] [0-9] * $ ");
                Return regex. IsMatch (inString. Trim ());
        }

This method can be used to determine whether it is a number or not. For example, to determine whether the E-mail format is correct, this method is supported as long as regular expressions are supported, you only need to replace the blue part of the regular expression in the code. You can refer to my BLOG for more information about regular expressions, which is quite comprehensive!

Example of several regular expressions:

"^ \ D + $" // non-negative integer (positive integer + 0)  
"^ [0-9] * [1-9] [0-9] * $" // positive integer      
"^ (-\ D +) | (0 +) $" // non-positive integer (negative integer + 0)      
"^-[0-9] * [1-9] [0-9] * $" // negative integer      
"^ -? \ D + $ "// integer      
"^ \ D + (\. \ d + )? $ "// Non-negative floating point number (Positive floating point number + 0)      
"^ ([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $"  
// Positive floating point number      
"^ (-\ D + (\. \ d + )?) | (0 + (\. 0 + )?)) $ "// Non-Positive floating point number (negative floating point number + 0)      
"^ (-([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $"  
// Negative floating point number      
"^ (-? \ D +) (\. \ d + )? $ "// Floating point number      
"^ [A-Za-z] + $" // A string consisting of 26 English letters      
"^ [A-Z] + $" // a string consisting of 26 uppercase letters      
"^ [A-z] + $" // a string consisting of 26 lowercase letters      
"^ [A-Za-z0-9] + $" // string consisting of digits and 26 letters      
"^ \ W + $" // a string consisting of digits, 26 letters, or underscores      
"^ [\ W-] + (\. [\ w-] +) * @ [\ w-] + (\. [\ w-] +) + $ "// email address      
"^ [A-zA-z] +: // (\ w + (-\ w + )*)(\. (\ w + (-\ w + )*))*(\? \ S *)? $ "// Url

Related Article

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.