Get Numeric From String

Niladri.Biswas
Posted by Niladri.Biswas under C# category on | Points: 40 | Views : 637
public static int GetNumericFromString(string data)
{
string retunrData = string.Empty;
int numericValue = -1;
data = data.Trim();
for (int iCount = 0; iCount < data.Length; iCount++)
{
string oneCharStr = data.Substring(iCount, 1);
char oneChar = Convert.ToChar(oneCharStr, CultureInfo.InvariantCulture);
if (Char.IsNumber(oneChar))
{
retunrData = retunrData + oneChar.ToString();
}
}
if (retunrData.Trim().Length > 0)
{
numericValue = Convert.ToInt32(retunrData, CultureInfo.InvariantCulture);
}
return numericValue;
}

Comments or Responses

Login to post response