【问题标题】:How to remove white space from Phone Number in iOS [duplicate]如何从iOS中的电话号码中删除空格[重复]
【发布时间】:2016-03-30 15:49:56
【问题描述】:

我无法从 iOS 应用中的电话号码中删除空格。

这是我的代码。

        ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
        for (CFIndex iPhone = 0; iPhone < ABMultiValueGetCount(multiPhones); iPhone++)
        {
            CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, iPhone);
            NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;
            if (phoneNumber == nil) {
                phoneNumber = @"";
            }
            if (phoneNumber.length == 0) continue;

            // phone number = (217) 934-3234
            phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
            phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
            phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
            phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];

            // phone number = 217 9343234
            [phoneNumbers addObject:phoneNumber];
        }

我希望得到没有空白。但它不会从电话号码中删除。 我该如何解决?请帮我。谢谢

【问题讨论】:

  • 嘿,特洛伊木马。我无法在任何博客上得到这个答案。为什么你标记为重复?
  • 因为您接受的答案提供了完全相同的解决方案!请在发帖前进行搜索,以免网站出现相同的问题。

标签: ios objective-c


【解决方案1】:

您可以使用NSCharacterSet 做一些比您目前正在做的事情更简单的事情。方法如下:

NSCharacterSet 定义一个字符集合。有一些标准的,例如decimalDigitsCharacterSetalphaNumericCharacterSet

还有一个称为invertedSet 的简洁方法,它返回一个字符集,其中包含当前字符中 的所有字符。现在,我们只需要一点信息。

NSString 有一个名为componentsSeparatedByCharactersInSet: 的方法,它会返回字符串部分的NSArray,围绕您提供的字符集中的字符进行分解。

NSArray 有一个补充函数componentsJoinedWithString:,您可以使用它来将数组的元素(返回)转换为字符串。看看这是怎么回事?

首先,定义一个我们想要包含在最终输出中的字符集:

NSCharacterSet *digits = [NSCharacterSet decimalDigitCharacterSet];

现在,把其他东西都拿来。

NSCharacterSet *illegalCharacters = [digits invertedSet]

一旦我们有了我们想要的字符集,我们就可以分解字符串并重构它:

NSArray *components = [phoneNumber componentsSeperatedByCharactersInSet:illegalCharacters];

NSString *output = [components componentsJoinedByString:@""];

这应该会给你正确的输出。四行,你就完成了:

NSCharacterSet *digits = [NSCharacterSet decimalDigitCharacterSet];
NSCharacterSet *illegalCharacters = [digits invertedSet];
NSArray *components = [phoneNumber componentsSeparatedByCharactersInSet:illegalCharacters];

NSString *output = [components componentsJoinedByString:@""];

您可以使用whitespaceCharacterSet 做一些类似于从字符串中修剪空白的操作。

NSHipster 也有 a great article 关于这个。

编辑:

如果您想包含其他符号,例如+ 前缀或括号,您可以使用characterSetWithCharactersInString: 创建自定义字符集。如果您有两个字符集,例如十进制数字和您创建的自定义字符集,您可以使用NSMutableCharacterSet 修改您必须包含其他字符的字符集。

【讨论】:

  • 哎呀,你去。
  • 这不起作用。此行发生错误。 NSArray *components = [phoneNumber componentsSeparatedByString:illegalCharacters];
  • 什么不起作用?它编译吗?它会给出不正确的输出吗?
  • 编译成功。但是这条线给了我这个错误。 *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFCharacterSet 长度]:无法识别的选择器发送到实例 0x7d13aa90”
  • @WilliamG。 componentsSeparatedByString?!?!
猜你喜欢
  • 2020-10-07
  • 2020-09-07
  • 2015-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-26
  • 1970-01-01
  • 2021-02-06
相关资源
最近更新 更多