I have two phone numbers. I want to compare them. One is unformatted and one is formatted, for example: "0501231234", "050-123-1234".
How can I compare them ( String.equals()
)?
I have two phone numbers. I want to compare them. One is unformatted and one is formatted, for example: "0501231234", "050-123-1234".
How can I compare them ( String.equals()
)?
The android.telephony.PhoneNumberUtils
class provides methods to compare phone numbers. The compare(String a, String b)
method usually suffices, as it will "return true if they're identical enough for caller ID purposes."
You need to strip the one with the "-" and then use equals
String s1 = "0501231234",s2 = "050-123-1234s";
boolean equal = s1.equals(s2.replaceAll("-", ""));