删除不需要的字符在iOS中

我正在填充地址簿中的所有联系人。 我必须发送到服务器只有电话号码没有国家代码和没有任何特殊字符和空格。纯粹的电话号码。 但我的地址簿电话号码是不同的格式,就像这些

"+91 99-70-044099", "*125#", "+91 8605-681220", "+919975806164", 9822771881, 02026697467, "+**62*+91675#", "+91 80-87177718", "\U202a+91 81 49 753637\U202c", "8308-412345", "094-19-241898", "*123*7#", "+6596913434", 7898989876565657 

为了使这些没有特殊字符和空格,我做了以下代码

 NSError *error; NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:&error]; NSUInteger phoneValidates = [detector numberOfMatchesInString:phoneNumber options:0 range:NSMakeRange(0, [phoneNumber length])]; // NSLog(@"res=%d",phoneValidates); if (phoneValidates) { NSLog(@"yes it is phone number %@",phoneNumber); NSString *str = [phoneNumber stringByReplacingOccurrencesOfString:@"+" withString:@"" ]; NSLog(@"after=%@",str); str = [str stringByReplacingOccurrencesOfString:@" " withString:@"" ]; NSLog(@"after=%@",str); str = [str stringByReplacingOccurrencesOfString:@"-" withString:@"" ]; NSLog(@"after=%@",str); str = [str stringByReplacingOccurrencesOfString:@"\\U202a" withString:@"" ]; NSLog(@"after=%@",str); str = [str stringByReplacingOccurrencesOfString:@"\\U202c" withString:@"" ]; NSLog(@"after=%@",str); [arr_contact addObject:str]; } 

即使通过上面的代码,数组包含以下格式的数字

 "\U202a918149753637\U202c", 

如何删除这些字符? 如何从这个只获得数字。
一些号码是国家代码,然后如何删除国家代码,我的问题是91是我的国家代码,但也有一些数字,以91开始即实数,而不是国家代码。 我也必须检查所有types的国家代码。

只获取数字:

 NSMutableString *result = [NSMutableString stringWithCapacity:phoneNumber.length]; NSScanner *scanner = [NSScanner scannerWithString:phoneNumber]; NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"]; while ([scanner isAtEnd] == NO) { NSString *buffer; if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) { [result appendString:buffer]; } else { [scanner setScanLocation:([scanner scanLocation] + 1)]; } } NSLog(@"%@", result); 
 NSString * strippedNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [mobileNumber length])]; 

试试下面的代码:

我这样做:

 NSMutableString *strippedString = [NSMutableString stringWithCapacity:10]; 

我结束了一个空string,但代码不会崩溃。

我不得不求助于老的C:

  for (int i=0; i<[phoneNumber length]; i++) { if (isdigit([phoneNumber characterAtIndex:i])) { [strippedString appendFormat:@"%c",[phoneNumber characterAtIndex:i]]; } }