Popular Posts

Friday, January 21, 2011

The Ultimate Phone Number Parsing method

The ToNumbers method basically takes a string and turns it into only numbers using the fact that numbers are characters between 47 and 58.

public static string toDataBasePhoneNumber(string phoneNumber)
{
String cleanPhone = String.Empty;
cleanPhone = ToNumbers(phoneNumber);
if (cleanPhone.StartsWith("1"))
cleanPhone = cleanPhone.Substring(1);
return cleanPhone;
}

public static string UltimateToNumbers(string phone)
{
string str = phone;

str = str.Trim().ToUpper();

string check = toDataBasePhoneNumber(str);
if (check.Length == 10)
return check;
else if (check.Length less than 3)
return str;

if (str.Length less than 10)
return str;

if (str.Contains("/"))
str = str.Substring(0, str.IndexOf("/"));
if (str.Contains(","))
str = str.Substring(0, str.IndexOf(","));
if (str.Contains(" OR"))
str = str.Substring(0, str.IndexOf(" OR"));

check = toDataBasePhoneNumber(str);
if (check.Length == 10)
return check;

if (str.Contains("EXT"))
str = str.Substring(0, str.IndexOf("EXT"));
else if (str.Contains("POSTE"))
str = str.Substring(0, str.IndexOf("POSTE"));
else if (str.Contains(" X"))
str = str.Substring(0, str.IndexOf(" X"));
else if (str.Contains(" #"))
str = str.Substring(0, str.IndexOf(" #"));

check = ToNumbers(str);
if (check.Length == 10)
return check;

if (str.Substring(9).Contains("("))
{
check = toDataBasePhoneNumber(str.Substring(9));
if (check.Length == 10)
return check;
}

str = FromLettersToNumbers(str);

return toDataBasePhoneNumber(str);
}

///
/// Takes any 1 character string and if the character is a letter it will change it to the number
/// that it would be on the keypad e.g. FREE = 3733.
///
/// A string converted
/// a string of all numbers
public static string FromLettersToNumbers(string numberWithUpperCaseCharacters)
{
string s = numberWithUpperCaseCharacters;

s = s.Replace("A", "2");
s = s.Replace("B", "2");
s = s.Replace("C", "2");
s = s.Replace("D", "3");
s = s.Replace("E", "3");
s = s.Replace("F", "3");
s = s.Replace("G", "4");
s = s.Replace("H", "4");
s = s.Replace("I", "4");
s = s.Replace("J", "5");
s = s.Replace("K", "5");
s = s.Replace("L", "5");
s = s.Replace("M", "6");
s = s.Replace("N", "6");
s = s.Replace("O", "6");
s = s.Replace("P", "7");
s = s.Replace("Q", "7");
s = s.Replace("R", "7");
s = s.Replace("S", "7");
s = s.Replace("T", "8");
s = s.Replace("U", "8");
s = s.Replace("V", "8");
s = s.Replace("W", "9");
s = s.Replace("X", "9");
s = s.Replace("Y", "9");
s = s.Replace("Z", "9");

return s;

}


Testing It:

string[] separator = new string[] { "<;>" };
string colonSeparatedNumbers = "866-SYNLawn (866-796-5296)<;>(250) 881-7684 / (250) 361-6121<;>780-818-5919 or 306-468-7044<;>1-888-SILPADA (745-7232)<;>(778)233-1235 / (604) 729-9787<;>Fax: (262) 246-4528<;>678-740-0800<;>+91 80 41571900,+91 98450 08720<;>1-868-638-2633 Ext. 2262<;>(604) 730-0321 / (604) 218-0108<;>1-918-712-RIBS<;>(888) 267-0986 x702<;>514 341-5544 poste 280<;>301-475-2200, Ext 100<;>215-541-3211 X108<;>(512)795-9684 ext.101<;>305-532-LIME (5463)<;>(450) 420-9333 #225<;>1-877-KOKOFIT<;>(800) CORN-COB<;>505-766-KEVA<;>403.207.5850 ext. 232<;>1-800-995-JUNK(5865)<;>1-866-35 JUNGA<;>Fax: 212.851.8198<;>513.831.2100<;>(323)936-3300 Ext. 107<;>(416)291-8551 / (647) 886-6863<;>1-888-Gel-Bedz (435-2339)<;>1-817-4161-INK (465)<;>(888) 64-LASER<;>513-563-8339, ext. 135<;>(877) GET-HSWT<;>(61) 02 9362 4<;>0000000<;>Schiller Park<;>1 905 599 6476<;>800-367-5159<;>(410) 822-8879<;>";
string[] spliter = colonSeparatedNumbers.Split(separator, StringSplitOptions.RemoveEmptyEntries);


foreach (String s in spliter)
{
Console.WriteLine("Number: {0} Converted to: -> {1}", s, Text.UltimateToNumbers(s));
}