尝试从文本中删除所有网址:
- (NSString *)cleanText:(NSString *)text{
NSString *string = @"This is a sample of a http://abc.com/efg.php?EFAei687e3EsA sentence with a URL within it.";
NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray *matches = [linkDetector matchesInString:string options:0 range:NSMakeRange(0, [string length])];
for (NSTextCheckingResult *match in matches) {
if ([match resultType] == NSTextCheckingTypeLink) {
NSString *matchingString = [match description];
NSLog(@"found URL: %@", matchingString);
string = [string stringByReplacingOccurrencesOfString:matchingString withString:@""];
}
}
NSLog(string);
return string;
}
但是string
返回不变(有匹配)。
更新:控制台输出:
found URL: <NSLinkCheckingResult: 0xb2b03f0>{22, 36}{http://abc.com/efg.php?EFAei687e3EsA}
2013-10-02 20:19:52.772
This is a sample of a http://abc.com/efg.php?EFAei687e3EsA sentence with a URL within it and a number 097843.
@Raphael Schweikert 完成的现成工作配方。