C # determine whether the string is a numeric string

Source: Internet
Author: User

In C # programming, we sometimes need to determine whether a string is a numeric string. We can implement this using the following two methods.
[Method 1]: Use the try {} catch {} statement.
We can try to convert string variables of the string type to the int type in the try statement block. If the string is not a numeric string, an exception is thrown, in this case, exceptions can be caught in the catch statement block. If an exception is found, it is not a numeric string.
We can convert the string type to the int type in the following three methods.
(1) int. Parse (string); www.2cto.com
(2) Convert. ToInt16 (string); // use Convert. ToInt32 () when the number of digits in a string is greater than 4 ()
(3) Convert. ToInt32 (string );

Add a text box TextBox1 and a button Button1. When you click the button, determine whether the content in the text box is a numeric string. If yes, the converted value is output.

 
Protected void button#click (object sender, EventArgs e)
{
String message = TextBox1.Text. Trim ();
Int result;
If (isNumberic (message, out result ))
{
String tt = "<script> alert ('matched successfully, the converted integer is" + result + "') </script> ";
Page. ClientScript. RegisterStartupScript (this. GetType (), "", tt );
}
Else
Page. ClientScript. RegisterStartupScript (this. GetType (), "", "<script> alert ('match failed! ') </Script> ");
}
Protected bool isNumberic (string message, out int result)
{
// Determine whether it is an integer string
// If yes, convert it to a number and set it to an output value of the out type. true is returned; otherwise, false is returned.
Result =-1; // The result is defined as out to output the value.
Try
{
// When the number string is less than 4, the following three types can be converted.
// If the number of digits exceeds 4, select Convert. ToInt32 () and int. Parse ()
 
// Result = int. Parse (message );
// Result = Convert. ToInt16 (message );
Result = Convert. ToInt32 (message );
Return true;
}
Catch
{
Return false;
}
}
The above method can change int to double, such as double. Parse (), so that you can determine whether it is a floating point string
[Method 2]: Use a regular expression to determine.
Use a regular expression to verify whether the string is a numeric string. We need to use the isMatch () method of the Regex class. This class is available in System. Text. RegularExpressions. You can use using System. Text. RegularExpressions and import the namespace to access the Regex class. You can also access it directly through System. Text. RegularExpressions. Regex.


 
Protected bool isNumberic (string message, out int result)
{
System. Text. RegularExpressions. Regex rex =
New System. Text. RegularExpressions. Regex (@ "^ \ d + $ ");
Result =-1;
If (rex. IsMatch (message ))
{
Result = int. Parse (message );
Return true;
}
Else
Return false;
}

Regular Expressions can be used to determine whether a match matches a string of six digits or an Email. Regular Expressions are a good method.
 
Protected void button#click (object sender, EventArgs e)
{
String message = TextBox1.Text. Trim ();
IsNumeric (message); // determines whether the string is an integer string of 5
}
Protected void isNumeric (string message)
{
If (message! = "" & Regex. IsMatch (message, @ "^ \ d {5} $ "))
{
// Succeeded
Page. ClientScript. RegisterStartupScript (this. GetType (), "", "<script> alert ('matched! It is indeed a five-digit integer string ') </script> ");
}
Else
// Failed
Page. ClientScript. RegisterStartupScript (this. GetType (), "", "<script> alert ('match failed! ') </Script> ");
}

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.