可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
This question already has answers here:
Closed 3 years ago.
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
回答1:
I looked at this as not getting rid of 'spaces' but being left with only decimal characters. This code did that for me:
phoneNumberString = [[phoneNumberString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
This takes out everything that's not a number 0-9.
Swift 4.1:
phoneNumberString = phoneNumberString.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")
回答2:
Try this:
NSString *cleaned = [[phoneNr componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@""];
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.
回答3:
Some iOS7 phone numbers are encoded with non-breaking space. Try this:
NSString* stringFormatted = [phoneNumber stringByReplacingOccurrencesOfString:@"\u00a0" withString:@""];
回答4:
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...
- Add a contact in iOS7 and Apple stores this: (555).555-5555 (where . is actually U00A0 or  )
- My app copies a contact's info in a CFStringRef from AddressBook (when NSLogged the . shows)
- CFStringRef is cast into NSString (NSLog now shows 555\U00a0555-5555)
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:
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"." withString:@""];
// where @"." was created by typing Option+Spacebar
回答5:
Another (very flexible) option is to use a regular expression. This allows you to retain the + or any other characters you want to remain.
let numberFromAddressBook = "+1 818 323 323 323"
let cleanNumber = numberFromAddressBook.stringByReplacingOccurrencesOfString("[^0-9+]", withString: "", options: NSStringCompareOptions.RegularExpressionSearch, range:nil)
"+1818323323323"
回答6:
The cleanest solution I'm using in my apps is:
NSMutableCharacterSet *phoneNubmerCharacterSet = [NSMutableCharacterSet characterSetWithCharactersInString:@"+"];
[phoneNubmerCharacterSet formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]];
NSString* phoneString = [[phoneString componentsSeparatedByCharactersInSet:[phoneNubmerCharacterSet invertedSet]] componentsJoinedByString:@""];
No "if" logic, keeps the + in number, removes all kind of random unwanted characters
回答7:
I made a small adjustment to the poster's answer in case someone wants to maintain the + at the begining of the number.
I made this small adjustment if you want to keep the plus after removing the spaces.
Boolean shouldAddPlus = NO;
if([[phoneNumber substringToIndex:1] isEqualToString:@"+"])
{
shouldAddPlus = YES;
}
phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
if(shouldAddPlus)
phoneNumber = [base stringByAppendingString:phoneNumber];
回答8:
The correct solution is to replace the occurences with a valid space
NSString *clean = [dirty stringByReplacingOccurrencesOfString:@"\u00a0" withString:@" "];
So you dont loose the space and the user sees what sees in other apps.