How to remove non numeric characters from phone number in objective-c?

感情迁移 提交于 2019-11-29 17:19:04

问题


This is my first attempt to make an ios app.

I'm using people picker to ask the user for a phone number, but when it retrieves with the code below, my NSString *phone apears like (0) 111192222-2222. I'm from Brazil and here the correct mask for mobile phone numbers is (01111) 92222-2222 (the 9 is optional, some numbers have others don't). How to fix this mask? Or remove it entirely?

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    ABMultiValueRef multiValue = ABRecordCopyValue(person, property);
    CFIndex index = ABMultiValueGetIndexForIdentifier(multiValue, identifier);
    NSString *phone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(multiValue, index);
    return NO;
}

回答1:


See this answer: https://stackoverflow.com/a/6323208/60488

Basically:

NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];

For your case you may want to remove the characters "-", "(" and ")" from the character set.




回答2:


You can use few methods of NSString and NSMutableString as :

NSString *phone=@"(0) 111192222-2222";
//I'm from Brazil and here the correct mask for mobile phone numbers is (01111) 92222-2222
NSMutableString *editPhone=[NSMutableString stringWithString:[phone stringByReplacingOccurrencesOfString:@")" withString:@""]];
editPhone=[NSMutableString stringWithString:[editPhone stringByReplacingOccurrencesOfString:@" " withString:@""]];

[editPhone insertString:@") " atIndex:6];


NSLog(@"%@",editPhone);//(01111) 92222-2222



回答3:


I think there are ways to solve this:

  1. Using NSRegularExpression to remove anything but numbers. You can see here or here to know how to validate phone number.
  2. Write your own scanner to remove characters you don't need. Remove blanks or remove all but numbers.
  3. Use the UITextFieldDelegate, write the textField:shouldChangeCharactersInRange:replacementString: method, check the replacement string if it is in the range of 0-9.

Hope helps.




回答4:


I would use Regular expression to validate the phone number instead of killing myself to make a custom keyboard, which functions can be changed by iOS updates. So, allow all characters and validate inside the code.




回答5:


SWIFT 5.0 solution

let purePhoneNumber = phoneNumber.replacingOccurrences(of: "[^0-9]", 
                                                       with: "", 
                                                       options: .regularExpression)

If you want to leave + sign in string, you can use regexp [^0-9+].



来源:https://stackoverflow.com/questions/15126175/how-to-remove-non-numeric-characters-from-phone-number-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!