0

I have some problem while searching the address book. I want to search for phone number using addressbook and compare to check if it matches to some other number.

The plain number to which the addressbook data is to be matched is just like this +358473028403 but when I retrieve the phone number from addressbook record, it comes in formatted like +358.473.028403. I tried many means of comparing these two string but match always fails. I used this code to filter the characters in the phone number received from the addressbook, but it does not seem to work.

  NSCharacterSet *toExclude = [NSCharacterSet characterSetWithCharactersInString:@"/.()- "];
  phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:toExclude] componentsJoinedByString: @""];

   BOOL match =  [phoneNumber isEqualToString:@"+358473028403"]

match is still NO.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235

1 Answers1

0

I tried your sample code and it works as expected, given +358.473.028403 as input.

NSString *phoneNumber = @"+358.473.028403";
NSCharacterSet *toExclude = [NSCharacterSet characterSetWithCharactersInString:@"/.()- "];
phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:toExclude] componentsJoinedByString:@""];
BOOL match =  [phoneNumber isEqualToString:@"+358473028403"];
NSLog(@"%@", match ? @"YES" : @"NO"); // => YES

The issues is very likely to be in the input string, which is probably different than you think.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235