我正在使用NSDataDetector解析文本并检索数字。下面是我的代码:
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber
error:&error];
NSArray *matches = [detector matchesInString:locationAndTitle options:0 range:NSMakeRange(0,[locationAndTitle length])];
for (NSTextCheckingResult *match in matches) {
if ([match resultType] == NSTextCheckingTypePhoneNumber) {
self.theNumber = [match phoneNumber];
}
}这样做的问题是,它有时会返回如下内容:
电话: 9729957777或9729957777x3547634
我不希望它出现,而且删除它比使用正则代码检索数字更难。你知道如何只检索号码吗。
发布于 2011-07-24 09:00:48
就我个人而言,我只是在字符串上使用-substringWithRange:来删除过去的所有内容,包括'x‘字符:
NSString * myPhoneNum = @"9729957777x3547634";
NSRange r = [myPhoneNum rangeOfString:@"x"];
if (r.location != NSNotFound) {
myPhoneNum = [myPhoneNum substringWithRange:NSMakeRange(0, r.location)];
}
NSLog(@"Fixed number: %@", myPhoneNum);不管怎样,你知道x3547634是从哪里来的吗?
https://stackoverflow.com/questions/6803608
复制相似问题