VB.NET has lots of functions that C# developers have to create manually.
Out of which I am providing here the different alternatives of IsNumeric function of VB.NET in C#
Using Parse
static bool IsNumeric(string s)
{
try
{
Int32.Parse(s);
}
catch
{
return false;
}
return true;
}
Using Double.TryParse
(C# 2.0 and above)
static bool IsNumeric(object expression)
{
if (expression == null)
return false;
double number;
return Double.TryParse(Convert.ToString(expression, CultureInfo.InvariantCulture),
System.Globalization.NumberStyles.Any, NumberFormatInfo.InvariantInfo, out number);
}
Using Regular Expression
bool IsNumeric(string value)
{
Regex regxNumericPatters = new Regex("[^0-9]");
return !regxNumericPatters.IsMatch(value);
}
OR
static bool IsNumeric(string inputString)
{
return Regex.IsMatch(inputString, "^[0-9]+$");
}
Using Char
static bool IsNumeric(string numberString)
{
foreach (char c in numberString)
{
if (!char.IsNumber(c))
return false;
}
return true;
}
I hope this will help you, if you have more ideas then do write comments.
0 comments:
Post a Comment