Use regualr expression to check: is it a number
using System;
using System.Data;
using System.Text.RegularExpressions;
class Class1{
static void Main(string[] args){
string IsNotNum = "111west";
string IsNum = " +111 ";
string IsFloat = " 23.11 ";
string IsExp = " +23 e+11 ";
Console.WriteLine(IsNumericRegEx(IsNum)); // True
Console.WriteLine(IsNumericRegEx(IsNotNum)); // False
Console.WriteLine(IsNumericRegEx(IsFloat)); // True
Console.WriteLine(IsNumericRegEx(IsExp)); // False
}
public static bool IsNumericRegEx(string str)
{
str = str.Trim();
return (Regex.IsMatch(str, @"^[\+\-]?\d*\.?[Ee]?[\+\-]?\d*$"));
}
}
Related examples in the same category