2011-07-23 202 views
1

我正在使用NSDataDetector解析文本並檢索數字。這裏是我的代碼: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

我不希望出現和刪除它會更難,然後使用正則表達式代碼來檢索數字。你有關於如何檢索只有號碼的任何想法。

回答

2

個人而言,我只想用-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從何而來,反正?