1

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() )?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
User
  • 305
  • 1
  • 3
  • 15

2 Answers2

3

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."

Mike M.
  • 38,532
  • 8
  • 99
  • 95
0

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("-", ""));
sotcha
  • 923
  • 7
  • 15