如何将电话号码和会议代码与字符串分开

时间:2015-07-16 07:21:52

标签: objective-c xcode string ios8 nsdatadetector

我有一个类似以下格式的字符串,

  

来电免费电话:180012345678(印度)

     

来电号码:+ 44-(0)123456789(英国)

     

英国免费电话:12345678910

     

example

     

会议代码:123 456 56789

如何在Objective-C

中将电话号码与此字符串分开

2 个答案:

答案 0 :(得分:3)

您可以使用NSDataDetector(https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSDataDetector_Class/

支持以下类型:

NSTextCheckingTypeOrthography        = 1ULL << 0,
NSTextCheckingTypeSpelling           = 1ULL << 1,
NSTextCheckingTypeGrammar            = 1ULL << 2,
NSTextCheckingTypeDate               = 1ULL << 3,
NSTextCheckingTypeAddress            = 1ULL << 4,
NSTextCheckingTypeLink               = 1ULL << 5,
NSTextCheckingTypeQuote              = 1ULL << 6,
NSTextCheckingTypeDash               = 1ULL << 7,
NSTextCheckingTypeReplacement        = 1ULL << 8,
NSTextCheckingTypeCorrection         = 1ULL << 9,
NSTextCheckingTypeRegularExpression  = 1ULL << 10
NSTextCheckingTypePhoneNumber        = 1ULL << 11,
NSTextCheckingTypeTransitInformation = 1ULL << 12

离。

NSError *error = NULL;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:&error];

NSUInteger numberOfMatches = [detector numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];

NSArray *matches = [detector matchesInString: stringoptions:0 range:NSMakeRange(0, [string length])];

for (NSTextCheckingResult *match in matches) {
    NSRange matchRange = [match range];
    if ([match resultType] == NSTextCheckingTypePhoneNumber) {
        NSString *phoneNumber = [match phoneNumber];
    }
}

答案 1 :(得分:0)

从另一个与您的问题非常相似的帖子中检查此代码:https://stackoverflow.com/a/20230333/1658831