extract all numeric values from a string using Linq

Have you ever wanted to get all the numeric values from a string?

One simple way is to iterate over  the string and evaluate each character but it require few lines of code so why not Linq !?

The following code extract all numeric values:

string str = "a12/343=";
int number = Convert.ToInt32(new string(str.Where(c => Char.IsDigit(c)).ToArray()));

the output will be: 12343

remember to import Linq reference:

using System.Linq;

You may also like...

1 Response

  1. July 5, 2014

    tnx for info!

Leave a Reply

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