如何从objective-c中的电话号码中删除非数字字符?

时间:2021-02-25 06:28:33

This is my first attempt to make an ios app.

这是我第一次尝试制作ios应用程序。

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?

我正在使用人员选择器向用户询问电话号码,但是当它使用下面的代码检索时,我的NSString *电话就像(0)111192222-2222一样。我来自巴西,这里正确的手机号码掩码是(01111)92222-2222(9是可选的,有些数字有其他人没有)。如何修复这个面膜?或者完全删除它?

- (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;
}

4 个解决方案

#1


40  

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

请参阅此答案:https://*.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


2  

You can use few methods of NSString and NSMutableString as :

您可以使用NSString和NSMutableString的几种方法:

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


2  

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. 使用NSRegularExpression删除除数字之外的任何内容。您可以在此处或此处查看如何验证电话号码。
  3. Write your own scanner to remove characters you don't need. Remove blanks or remove all but numbers.
  4. 编写自己的扫描仪以删除不需要的字符。删除空白或删除所有数字。
  5. Use the UITextFieldDelegate, write the textField:shouldChangeCharactersInRange:replacementString: method, check the replacement string if it is in the range of 0-9.
  6. 使用UITextFieldDelegate,编写textField:shouldChangeCharactersInRange:replacementString:方法,检查替换字符串是否在0-9范围内。

Hope helps.

希望有所帮助

#4


1  

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.

我会使用正则表达式来验证电话号码而不是自杀以制作自定义键盘,这些功能可以通过iOS更新来更改。因此,允许所有字符并在代码内验证。

#1


40  

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

请参阅此答案:https://*.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


2  

You can use few methods of NSString and NSMutableString as :

您可以使用NSString和NSMutableString的几种方法:

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


2  

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. 使用NSRegularExpression删除除数字之外的任何内容。您可以在此处或此处查看如何验证电话号码。
  3. Write your own scanner to remove characters you don't need. Remove blanks or remove all but numbers.
  4. 编写自己的扫描仪以删除不需要的字符。删除空白或删除所有数字。
  5. Use the UITextFieldDelegate, write the textField:shouldChangeCharactersInRange:replacementString: method, check the replacement string if it is in the range of 0-9.
  6. 使用UITextFieldDelegate,编写textField:shouldChangeCharactersInRange:replacementString:方法,检查替换字符串是否在0-9范围内。

Hope helps.

希望有所帮助

#4


1  

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.

我会使用正则表达式来验证电话号码而不是自杀以制作自定义键盘,这些功能可以通过iOS更新来更改。因此,允许所有字符并在代码内验证。