How to check if a string is a number in C#
Steps to check if a string is a number in C#
- Declare an integer variable.
- Pass string to
int.TryParse()
ordouble.TryParse()
methods without
variable. - If the string is a number
TryParse
method will return true. And assigns value to the declared integerout
value.
On this page
Check if a string is a Number or not in C#
For example we have a string variable “123” and if you want to check whether it is numeric use the below C# code.
var stringNumber = "123";
int numericValue;
bool isNumber = int.TryParse(stringNumber, out numericValue);
//isNumber is true and numericValue=123
var stringNumber = "123P";
int numericValue;
bool isNumber = int.TryParse(stringNumber, out numericValue);
//isNumber is false and numericValue=0 default value
From C# 7 onwards we can declare out variable in TryParse Method itself.
bool isNumber = int.TryParse(stringNumber, out int numericValue);
The problem with the above int.TryParse
method is it cannot check for negative string number values.
Checking for negative string number in C#
To Check for negative string number values we can use C# double.TryParse()
method.
var negativeString = "-123";
double number;
if(double.TryParse(negativeString,out number)){
if (number > 0){
}else{
//negative number
}
}
Best way to check if string is number in C#
Always use double.TryParse()
method to check if a string is number, because it can validate both positive and negative numbers.