Definitely regex:
string CleanPhone(string phone)
{
Regex digitsOnly = new Regex(@"[^d]");
return digitsOnly.Replace(phone, "");
}
or within a class to avoid re-creating the regex all the time:
private static Regex digitsOnly = new Regex(@"[^d]");
public static string CleanPhone(string phone)
{
return digitsOnly.Replace(phone, "");
}
Depending on your real-world inputs, you may want some additional logic there to do things like strip out leading 1's (for long distance) or anything trailing an x or X (for extensions).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…