Use int.Parse to check: is it an integer

using System;
using System.Data;
class Class1{
static void Main(string[] args){
string IsNotNum = "111west";
string IsNum = " +111 ";
string IsFloat = " 23.11 ";
string IsExp = " +23 e+11 ";
Console.WriteLine(IsNumeric(IsNum)); // True
Console.WriteLine(IsNumeric(IsNotNum)); // False
Console.WriteLine(IsNumeric(IsFloat)); // False
Console.WriteLine(IsNumeric(IsExp)); // False
Console.WriteLine();
}
public static bool IsNumeric(string str)
{
try
{
str = str.Trim();
int foo = int.Parse(str);
return (true);
}
catch (FormatException e)
{
Console.WriteLine("Not a numeric value: {0}", e.ToString());
return (false);
}
}
}
Related examples in the same category