Asp.net check whether the verification string is a pure numeric method summary

Source: Internet
Author: User

In asp.net, we verify whether the string is a number. We don't use as many rich functions as php. Here I have sorted out some examples to verify whether the string is a pure numeric method code.

Example 1

The Code is as follows: Copy code


# Region's method of determining whether a number is used
Public bool isnumeric (string str)
{
Char [] ch = new char [str. Length];
Ch = str. ToCharArray ();
For (int I = 0; I <ch. Length; I ++)
{
If (ch [I] <48 | ch [I]> 57)
Return false;
}
Return true;
}
# Endregion

Example 2

The Code is as follows: Copy code

Class IsNumeric
{
// Determine whether the string is a pure number
Public static bool IsNumber (string str)
{
If (str = null | str. Length = 0) // verify whether this parameter is null.
Return false; // Yes, False is returned.
ASCIIEncoding ascii = new ASCIIEncoding (); // instance of new ASCIIEncoding
Byte [] bytestr = ascii. GetBytes (str); // Save the string type parameter to the array

Foreach (byte c in bytestr) // traverses the content in this array
{
If (c <48 | c> 57) // determines whether it is a number.
{
Return false; // No, False is returned.
}
}
Return true; // Yes, True is returned.
}
}

The above two instances are simple: Get the character type and traverse it to determine if it is a number.

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.