The best ans as you need.
If you want to numbers from string just simply use regex.
See the following code block.
Regex reges = new Regex(@"\d+");
Match match = regex.Match("test 66");
if (match.Success)
{
Console.WriteLine(string.Format("RegEx found " + match.Value + " at position " + match.Index.ToString()));
}
else
{
Console.WriteLine("You didn't enter a string containing a number!");
}
and also small amount of code will helps you.
result = Regex.Match(FeatchFromStr, @"\d+").Value;
And in very old trick to solve this if you not want to use regex.
string source = "str123";
string destination = string.Empty;
int FinalResult;
for (int i=0; i< source.Length; i++)
{
if (Char.IsDigit(source[i]))
destination += source[i];
}
if (destination.Length>0)
FinalResult = int.Parse(destination);
Even linq can also help you.
string Source = "3241k2h341k2341khlkhjl";
var Numbers = (from s in Source
where char.IsDigit(s)
select s).ToArray();
Console.WriteLine(new string(Numbers));