Determines whether the first digit of a string is a number
The code is as follows |
Copy Code |
Using System; Using System.Collections.Generic; Using System.ComponentModel; Using System.Data; Using System.Drawing; Using System.Linq; Using System.Text; Using System.Windows.Forms; Using System.Text.RegularExpressions; Namespace WindowsFormsApplication1 { public partial class Form1:form { Public Form1 () { InitializeComponent (); } private void Button1_Click (object sender, EventArgs e) { Regex Regchina = new Regex ("^[^x00-xff]"); Regex regnum = new Regex ("^[0-9]"); Regex Regchar = new Regex ("^[a-z]"); Regex Regdchar = new Regex ("^[a-z]"); String str = "yl Len Lei"; if (Regnum.ismatch (str)) { MessageBox.Show ("is a number"); } else if (Regchina.ismatch (str)) { MessageBox.Show ("is Chinese"); } else if (Regchar.ismatch (str)) { MessageBox.Show ("lowercase"); } else if (Regdchar.ismatch (str)) { MessageBox.Show ("uppercase"); } } } } |
Determines whether a string is a numeric string
Method One: Use the try{} catch{} statement.
We can try to convert a string variable of type string to an int type in a try statement block, and if the string is not a numeric string, an exception is thrown, and an exception can be caught in a catch statement block. Once an exception is found, it is not a numeric string.
We can convert a string type to an int type in the following three ways.
(1) Int. Parse (string);
(2) convert.toint16 (string); Use Convert.ToInt32 when the number of digits in the numeric string is greater than 4 ()
(3) Convert.ToInt32 (string);
Add a text box TextBox1, and a button Button1, when the button is clicked, to determine whether the text box is a numeric string, is the output of the converted value.
The code is as follows |
Copy Code |
protected void Button1_Click (object sender, EventArgs e) { String message = TextBox1.Text.Trim (); int result; if (isnumberic (message,out result)) { String tt= "<script>alert (' match succeeded, converted integer as" +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) { Determines whether an integer string Yes, convert it to a number and set it to an output value of out type, return true, or false result =-1; Result defined as out to output value Try { When the number string is less than 4 o'clock, all of the following three can be converted, optional If the number of digits exceeds 4, choose Convert.ToInt32 () and Int. Parse ()
result = Int. Parse (message); result = convert.toint16 (message); result = Convert.ToInt32 (message); return true; } Catch { return false; } } |