【问题标题】:iPhone address book - get phone number as string with numbers onlyiPhone通讯录 - 将电话号码作为字符串获取,仅包含数字
【发布时间】:2014-09-20 17:25:11
【问题描述】:

我从通讯录中获取了一个电话号码,并将它放在一个 NSString* 中:

ABMultiValueRef phoneProperty = ABRecordCopyValue(person,property);
NSString *phone = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneProperty,identifier);

我只想将数字作为直字符串获取(意思是没有 (,)、+、- 等字符)。

当我执行上面的代码并检查它NSLog(@"phone is %@", phone); 我得到:phone is 1 (800) 800-8000

实际上,在 Xcode 控制台中,我看到在 '(' 之前和 ')' 之后有一个中心点符号,它没有正确复制到这篇文章中。

我的目标是将1 (800) 800-8000 变成这个18008008000。我该怎么做?

【问题讨论】:

  • 我没有答案,但请记住,如果您打算支持本地化,有许多不同的格式。

标签: ios abaddressbook number-formatting phone-number


【解决方案1】:

您可以通过多种方式做到这一点。

一种简单的方法:

NSString *phone = @"1 (800) 800-8000 home";
NSString *strippedNumber = [phone stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [phone length])];

注意: [^0-9] 表示 0123456 之外的任何字符、789

【讨论】:

    【解决方案2】:

    使用以下代码:

    NSString *filtered = [[phone componentsSeparatedByCharactersInSet:
        [[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
                                             componentsJoinedByString:@""];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-10
      • 2011-07-19
      • 2013-11-14
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      相关资源
      最近更新 更多