Ios iPhone通讯簿-获取电话号码作为字符串,仅包含数字
我从通讯簿中获得一个电话号码,并将其放入NSString*:Ios iPhone通讯簿-获取电话号码作为字符串,仅包含数字,ios,abaddressbook,number-formatting,phone-number,Ios,Abaddressbook,Number Formatting,Phone Number,我从通讯簿中获得一个电话号码,并将其放入NSString*: ABMultiValueRef phoneProperty = ABRecordCopyValue(person,property); NSString *phone = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneProperty,identifier); 我只想得到一个直字符串形式的数字(意思是没有像(,),+,-这样的字符) 当我执行上面的代码并
ABMultiValueRef phoneProperty = ABRecordCopyValue(person,property);
NSString *phone = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneProperty,identifier);
我只想得到一个直字符串形式的数字(意思是没有像(,),+,-这样的字符)
当我执行上面的代码并检查它NSLog(@“phone is%@”,phone)代码>我得到:电话号码是1(800)800-8000
实际上,在Xcode控制台中,我看到在“(”和“)”之前有一个中心点符号,它没有正确地复制到本文中
我的目标是把1(800)800-8000
变成这个18008008000
。如何操作?使用以下代码:
NSString *filtered = [[phone componentsSeparatedByCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
componentsJoinedByString:@""];
使用以下代码:
NSString *filtered = [[phone componentsSeparatedByCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
componentsJoinedByString:@""];
你可以用很多方法来做
一个简单的方法:
NSString *phone = @"1 (800) 800-8000 home";
NSString *strippedNumber = [phone stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [phone length])];
注意:[^0-9]
是指除0
、1
、2
、3
、4
、5
、6
、7
、8
、或9
之外的任何字符。您可以通过多种方式来完成
一个简单的方法:
NSString *phone = @"1 (800) 800-8000 home";
NSString *strippedNumber = [phone stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [phone length])];
注:[^0-9]
指除0
、1
、2
、3
、4
、5
、6
、7
、8
、或9
之外的任何字符。我没有答案,但是请记住,如果您打算支持本地化,有许多不同的格式。我没有答案,但请记住,如果您打算支持本地化,有许多不同的格式。