C# – Retrieve only numbers in a string

I wanted to retrieve only the number in a string to clean up some telephone numbers users have entered in a system today. So I came across with this nice Regular Expression it is really simple and most importantly it was the fastest method I could found. Here is the code;

public String returnonlyNumbers(String stringValue)
           return string.Join(null, System.Text.RegularExpressions.Regex.Split(stringValue, "[^\\d]"));

The above code would return only the numbers in any given string. For example if you pass the string  “+94 (084) 849748 489748”  to this method it would return “94084849748489748”.

Hope someone would find this beneficial.



Blog Directory - OnToplist.com