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