CODEDIGEST
Home » CodeDigest
Search
 

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!");
        }

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..