CODEDIGEST
Home Articles CodeDigest Tutorials FAQs
Home > CodeDigest >  
Technologies
 


How to check if a String is Integer in C#?
Submitted By Bala Murugan
On 10/30/2010 8:27:51 AM
Tags: C#,CodeDigest  

How to check if a String is Integer in C#?

 

There will be scenarios where we will require validating if a given string is an integer.
The below code will help us to do the same.


string number = "100";
int no = 0;
if (int.TryParse(number, out no))
        {
Response.Write(no.ToString() + " is a valid number!");
        }
else
        {
Response.Write(no.ToString() + " is not a valid number!");
        }

 

You can also do this validation using regular expression. Refer below,


string number = "100";      
if (Regex.IsMatch(number, @"\d"))
        {
Response.Write(no.ToString() + " is a valid number!");
        }
else
        {
Response.Write(no.ToString() + " is not a valid number!");
        }

Recent Codes
  • View All Codes..