Quick and simple way to parse numbers in string

Some times we have to pull numbers from mixed string. There are several ways to do it, so here i will indroduce some easy and quick one 🙂

string fullSentence ="Just a sentence with numbers from 1 asd f3 234 6to10111"
string numbers = Regex.Split(fullSentetnce @"D+"); 

foreach(string number in numbers)
{
  if(!string.IsNullOrEmpty(number)
  {
    int i =int.Parse(number);
    Console.WriteLine("{0}",i); 
  }
}

Result:

Leave a Reply

Your email address will not be published. Required fields are marked *