In every country phone numbers are represented
in its own format. For example : In some country a phone number could be in
(914) 575-4231 format whereas in another country the same number could be in
914-575-42131 format. So if we want to compare two numbers represented in
different format, we could do this by removing their formatting and compare
their actual value.
Here is the sample code:
using System;
using System.Collections;
class Program
{
static void Main()
{
string num1 = "800-555-1212"; // Format -1
string num2 = "(800) 555 1212"; // Format -2
if (FetchDigitsOnlyFromPhoneNumber(num1) ==
FetchDigitsOnlyFromPhoneNumber(num2))
{
Console.WriteLine("Number in both the formats are Equal !");
}
else
{
Console.WriteLine("Unequal Numbers !");
}
Console.Read();
}
private static string FetchDigitsOnlyFromPhoneNumber(string formattedNumber)
{
string actualNumber;
actualNumber = formattedNumber;
actualNumber = actualNumber.Replace("(", string.Empty);
actualNumber = actualNumber.Replace(")", string.Empty);
actualNumber = actualNumber.Replace("+", string.Empty);
actualNumber = actualNumber.Replace("-", string.Empty);
actualNumber = actualNumber.Replace(" ", string.Empty); //Blank
actualNumber = actualNumber.Replace(".", string.Empty);
return actualNumber;
}
}

Join the conversation! Your thoughts help the community grow.