0

I can't remove white space from Phone Number in iOS app.

Here is my codes.

        ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
        for (CFIndex iPhone = 0; iPhone < ABMultiValueGetCount(multiPhones); iPhone++)
        {
            CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, iPhone);
            NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;
            if (phoneNumber == nil) {
                phoneNumber = @"";
            }
            if (phoneNumber.length == 0) continue;

            // phone number = (217) 934-3234
            phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
            phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
            phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
            phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];

            // phone number = 217 9343234
            [phoneNumbers addObject:phoneNumber];
        }

I expect to get without white space. But it is not removed from the phone number. How can I fix? Please help me. Thanks

William G.
  • 393
  • 2
  • 6
  • 11
  • Hey, trojanfoe. I couldn't get this answer on any blogs. Why did you mark as duplicated? – William G. Mar 30 '16 at 16:33
  • Because the answer you accepted provides the exact same solution! Please search before posting to avoid filling the site with the same questions. – trojanfoe Mar 30 '16 at 17:14

1 Answers1

0

You can do something a lot simpler than what you're currently doing with NSCharacterSet. Here's how:

NSCharacterSet defines a collection of characters. There are a few standard ones, such as decimalDigitsCharacterSet and alphaNumericCharacterSet.

There's also a neat method called invertedSet which returns a character set with all of the characters not included in the current one. Now, we need just one more bit of information.

NSString has a method called componentsSeparatedByCharactersInSet:, which gives you back an NSArray of the parts of the string, broken up around the characters in the characterSet you supply.

NSArray has a complementary function, componentsJoinedWithString: which you can use to turn the elements of an array (back) into a string. See where this is going?

First, define a character set that we want to include in our final output:

NSCharacterSet *digits = [NSCharacterSet decimalDigitCharacterSet];

Now, get everything else.

NSCharacterSet *illegalCharacters = [digits invertedSet]

Once we have the character set that we want, we can break out the string and reconstruct it:

NSArray *components = [phoneNumber componentsSeperatedByCharactersInSet:illegalCharacters];

NSString *output = [components componentsJoinedByString:@""];

That should give you the correct output. Four lines, and you're done:

NSCharacterSet *digits = [NSCharacterSet decimalDigitCharacterSet];
NSCharacterSet *illegalCharacters = [digits invertedSet];
NSArray *components = [phoneNumber componentsSeparatedByCharactersInSet:illegalCharacters];

NSString *output = [components componentsJoinedByString:@""];

You can use the whitespaceCharacterSet do do something similar to trim whitespace off of strings.

NSHipster has a great article about this, too.

EDIT:

If you want to include other symbols, such as the + prefix or parenthesis, you can create custom character sets with characterSetWithCharactersInString:. If you have two character sets, such as the decimal digits and the custom one you created, you could use NSMutableCharacterSet to modify the character set you have to include other characters.

Moshe
  • 57,511
  • 78
  • 272
  • 425
  • Whoops, there you go. – Moshe Mar 30 '16 at 15:58
  • This is not working. An error occurred on this line. NSArray *components = [phoneNumber componentsSeparatedByString:illegalCharacters]; – William G. Mar 30 '16 at 16:07
  • What's not working? Does it compile? Does it give incorrect output? – Moshe Mar 30 '16 at 16:07
  • It is compiled successfully. But this line gives me this error. *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFCharacterSet length]: unrecognized selector sent to instance 0x7d13aa90' – William G. Mar 30 '16 at 16:08
  • @WilliamG. `componentsSeparatedByString`?!?! – trojanfoe Mar 30 '16 at 16:09
  • When I use componentsSeperatedByCharactersInSet. It gives me compile error. No such method. – William G. Mar 30 '16 at 16:10
  • There's definitely a `componentsSeparatedByCharactersInSet` method. I had the same issue when copying my code that I typed here into Xcode. You might have to let autocomplete type it out for you. Let me put some documentation links into my answer. – Moshe Mar 30 '16 at 16:14
  • You spelled it wrong in your code snippet (seperated vs separated) – dan Mar 30 '16 at 16:14
  • That makes sense - whoops. – Moshe Mar 30 '16 at 16:15
  • Thank you Moshe. It is working well. But there was a spell error. componentsSeperatedByCharactersInSet should be replaced into componentsSeparatedByCharactersInSet in your code. This is the best answer :) Thank you very much, Moshe. But one more problem, if there is a + prefix in phone number, I want to keep it. I want to remove only (, ), - and white space. Could you help me? – William G. Mar 30 '16 at 16:19
  • You're welcome. (If you like this answer and you think I deserve it, please use the checkmark to indicate this is correct.) For your other problem, you can use NSCharacterSet to create a new set with that `+` character in it. Then, make a union of that set with the `decimalDigitsCharacterSet`. *Then* invert the set and continue as we did earlier. – Moshe Mar 30 '16 at 16:20
  • Could you leave exact code here? :( – William G. Mar 30 '16 at 16:21