Aug032009

Sanitizing Phone Numbers With C#

An extension method to remove all but digits from a phone number. This should be useful for sanitizing input to put into your database.

using System.Text.RegularExpressions;

namespace CustomStringMethods {
    public static class PhoneNumber {
        public static string StripAllButDigits(this string s) {
            return (s == null) ? string.Empty : Regex.Replace(s, @"\\D", string.Empty);
        }

    }
}

Obviously this can be used for more than just phone numbers.