I reading data from webservice and is not clean. I need to convert string to Int where string can be null, number or with white spaces. I make simple program to achieve this but with whitespaces my code does not hit ... If (uint.TryParse(cleanNumber, out ux)) not sure what I am missing from puzzle?
public class Program
{
static void Main(string[] args)
{
string no = "08709079777 ";
//string no = "0870777777";
Console.WriteLine("no "+ no);
Program o = new Program();
var t1 = o.ConvertStringToInt32(no);
Console.WriteLine("t1 "+ t1);
Console.ReadLine();
}
private int ConvertStringToInt32(string number)
{
int returnIntVal = 0;
try
{
if (!string.IsNullOrEmpty(number))
{
var cleanNumber = Regex.Replace(number, @"\s+", "");
uint ux;
if (uint.TryParse(cleanNumber, out ux))
{
returnIntVal = (int)ux;
}
}
else
{
returnIntVal = 0;
}
}
catch(Exception exp)
{
var ex = exp;
}
return returnIntVal;
}
}