C # determine whether the input text is a number)

Source: Internet
Author: User

Asp.net determines whether the input text is a number
Solution 1:
/** // <Summary>
/// Name: isnumberic
/// Function: determines whether the input is a number.
/// Parameter: String otext: Source Text
/// Return value: bool true: false: No
/// </Summary>
Public bool isnumberic (string otext)
{
Try
{
Int var1 = convert. toint32 (otext );
Return true;
}
Catch
{
Return false;
}
}

Try catch Method
Example:
Try
{
Convert. toint32 ("123 "):
Console. Write ("yes ");
}
Catch (exception ex)
{
Console. Write ("non-numeric ");
}
NOTE: If many strings require judgment, This method requires a lot of try catch and finally to process subsequentProgram. This method is not recommended.

Improvement:
Because int can be converted to decimal.
Public bool isnumberic (string otext)
{
Try
{
Decimal number = convert. todecimal (otext );
Return true;
}
Catch
{
Return false;
}
}

Solution 2:
// If it is a pure number, you can use ASCII code for determination.
/// <Summary>
/// Determine whether it is a number
/// </Summary>
/// <Param name = "str"> string </param>
/// <Returns> bool </returns>
Public bool isnumeric (string Str)
{
If (STR = NULL | Str. Length = 0)
Return false;
System. Text. asciiencoding ASCII = new system. Text. asciiencoding ();
Byte [] bytestr = ASCII. getbytes (STR );
Foreach (byte C in bytestr)
{
If (C <48 | C> 57)
{
Return false;
}
}
Return true;
}
Solution 3:
Regular Expression Method
Example:
// Reference the regular expression class
Using system. Text. regularexpressions;
RegEx Reg = new RegEx ("^ [0-9] + $ ");
Match MA = reg. Match (text );
If (MA. Success)
{
// A number
}
Else
{
// Not a number
}
Note: This method is quick but not easy to understand, especially regular expression formulas. If you are interested, you can study it well. It is very useful and is recommended.
Solution 4:
Double. tryparse Method
Example:
Bool isnum = system. Double. tryparse ("the string to be judged", system. Globalization. numberstyles. integer, null, out );
Note: This method is fast, convenient, and easy to master. However, there are many parameters. If you are interested, you can study it. We recommend that you use it.
The parameter is not easy to use.
Not used
Method 5:
Create a class
Using system;
Using system. Collections. Generic;
Using system. Text. regularexpressions;
Namespace LBC. Number
{
/// <Summary>
/// Numeric judgment class
/// </Summary>
Public class numberclass
{
/// <Summary>
/// Determine whether it is a number
/// </Summary>
/// <Param name = "strnumber"> string to be judged </param>
/// <Returns> </returns>
Public static bool isnumber (string strnumber)
{
RegEx objnotnumberpattern = new RegEx ("[^ 0-9.-]");
RegEx objtwodotpattern = new RegEx ("[0-9] * [.] [0-9] * [.] [0-9] *");
RegEx objtwominuspattern = new RegEx ("[0-9] * [-] [0-9] * [-] [0-9] *");
String strvalidrealpattern = "^ ([-] | [.] | [-.] | [0-9]) [0-9] * [.] * [0-9] + ___ fckpd ___ 0 quot ;;
String strvalidintegerpattern = "^ ([-] | [0-9]) [0-9] * ___ fckpd ___ 0 quot ;;
RegEx objnumberpattern = new RegEx ("(" + strvalidrealpattern + ") | (" + strvalidintegerpattern + ")");
Return! Objnotnumberpattern. ismatch (strnumber )&&
! Objtwodotpattern. ismatch (strnumber )&&
! Objtwominuspattern. ismatch (strnumber )&&
Objnumberpattern. ismatch (strnumber );
}
/// <Summary>
/// Determine whether it is of the int type
/// </Summary>
/// <Param name = "value"> string to be judged </param>
/// <Returns> </returns>
Public static bool isint (string value)
{
Return RegEx. ismatch (value, @ "^ [+-]? \ D * ___ fckpd ___ 0 quot ;);
}
/// <Summary>
/// Determine whether it is a number
/// </Summary>
/// <Param name = "value"> string to be judged </param>
/// <Returns> </returns>
Public static bool isnumeric (string value)
{
Return RegEx. ismatch (value, @ "^ [+-]? \ D * [.]? \ D * ___ fckpd ___ 0 quot ;);
}
}
}

 

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.