1.

How Do You Determine Whether A String Represents A Numeric Value?

Answer»

To determine whether a STRING represents a numeric value use TryParse method as shown in the example below. If the string contains nonnumeric characters or the numeric value is too large or too small for the particular type you have SPECIFIED, TryParse returns false and sets the out parameter to zero. OTHERWISE, it returns true and sets the out parameter to the numeric value of the string.

string str = "One";
int i = 0;
if(int.TryParse(str,out i))
{
Console.WriteLine("Yes string contains Integer and it is " + i);
}
else
{
Console.WriteLine("string does not CONTAIN Integer");
}

To determine whether a String represents a numeric value use TryParse method as shown in the example below. If the string contains nonnumeric characters or the numeric value is too large or too small for the particular type you have specified, TryParse returns false and sets the out parameter to zero. Otherwise, it returns true and sets the out parameter to the numeric value of the string.

string str = "One";
int i = 0;
if(int.TryParse(str,out i))
{
Console.WriteLine("Yes string contains Integer and it is " + i);
}
else
{
Console.WriteLine("string does not contain Integer");
}



Discussion

No Comment Found