C # Determines whether a string is a number or a string that begins with a number

Source: Internet
Author: User
Tags numeric

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;
}
}
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.