Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to strip away letters from string?

Tags:

c#

regex

I want to strip away all letters in a string, which are not numeric. Preferably a solution made with Regular Expressions or something. And in C#. How to do that?

like image 480
Jaffa Avatar asked Oct 15 '22 04:10

Jaffa


1 Answers

Using Regex:

str = Regex.Replace(str, @"\D+", "");

\D is the complement of \d - matches everything that isn't a digit. + will match one or more of them (it usually works a little better than one by one).

Using Linq (on .Net 4.0):

str = String.Concat(str.Where(Char.IsDigit));
like image 190
Kobi Avatar answered Oct 25 '22 12:10

Kobi