What should be the best method to match phone numbers in situation where the format of phone number will be a problem?
I am looking for a standards rich best practices way of doing this. I have users type in a phone number and save to the SQLite DB. Later on a point of time, when a phone calls comes, I get the incomingNumber through ITelephony. I have to match the value in db with the incomingNumber and take an action. The formats of these numbers may differ greatly and may cause problems.
To get around this problem, I am using regex right now to eliminate formatting characters and leave only digits to get a proper match. This is working fine but I wanted to know if someone’s got a better way to do it, or if Android has an API Method to match two phone numbers.
Thanks for your interest
EDIT: The code that I am using is:
private boolean matchPhoneNumbers(String numberA, String numberB)
{
//Remove everything that's not a number
numberA=numberA.replaceAll("[^0-9]", "");
numberB=numberB.replaceAll("[^0-9]", "");
//Get the minimum length of one of the strings
int length=Math.min(numberA.length(),numberB.length());
numberA=numberA.substring((numberA.length()-length));
numberB=numberB.substring((numberB.length()-length));
//check the difference
return numberA.equals(numberB);
}
Go over each char and get rid of any char which is less than 0 or greater than 9. ( easy while loop) Than check if 10 chars from the right is the same or not.