In my app I am trying to retrieve the list of contact's number and try to do operations on them. I realized that whenever I have added new contacts (after updating to iOS 7) the new contacts formatting has changed, as there are spacings in the newly added numbers.
Using the ordinary replace methods does not remove the spaces.
Are these really spaces or what are these ? my objective is to get back the 'space' free number.
for example, if the number is 1 818 323 323 323
, I want to get 1818323323323
The correct solution is to replace the occurences with a valid space
So you dont loose the space and the user sees what sees in other apps.
After way too much string cleaning I finally found an answer after printing the CFStringRef straight from the Address Book. Here's what's going on behind the scenes...
To remove the \U00A0 I tried 3 answers from this thread and [NSCharacterSet characterSetWithRange:(160,1)] which didn't work. What finally worked was this line of code:
// where @"." was created by typing Option+Spacebar
Another (very flexible) option is to use a regular expression. This allows you to retain the + or any other characters you want to remain.
I looked at this as not getting rid of 'spaces' but being left with only decimal characters. This code did that for me:
This takes out everything that's not a number 0-9.
Swift 4.1:
The cleanest solution I'm using in my apps is:
No "if" logic, keeps the + in number, removes all kind of random unwanted characters
Try this:
This should work for any kind of space (of which there are many). It may seem somewhat inefficient, but for phone numbers, this should be ok.