1

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

拨打免费电话:180012345678(印度)

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

英国免费电话:12345678910

https://www.google.com/

会议代码:123 456 56789

如何在 Objective-C 中将电话号码与此字符串分开

4

2 回答 2

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];
    }
}
于 2015-07-16T07:27:41.477 回答
0

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

于 2015-07-16T07:30:09.060 回答