過濾字符串中的html標簽的方法:
[cpp]
- (NSString *)filterHtmlTag:(NSString *)originHtmlStr{
NSString *result = nil;
NSRange arrowTagStartRange = [originHtmlStr rangeOfString:@"<"];
if (arrowTagStartRange.location != NSNotFound) { //如果找到
NSRange arrowTagEndRange = [originHtmlStr rangeOfString:@">"];
// NSLog(@"start-> %d end-> %d", arrowTagStartRange.location, arrowTagEndRange.location);
// NSString *arrowSubString = [originHtmlStr substringWithRange:NSMakeRange(arrowTagStartRange.location, arrowTagEndRange.location - arrowTagStartRange.location)];
result = [originHtmlStr stringByReplacingCharactersInRange:NSMakeRange(arrowTagStartRange.location, arrowTagEndRange.location - arrowTagStartRange.location + 1) withString:@""];
// NSLog(@"Result--->%@", result);
return [self filterHtmlTag:result]; //遞歸,過濾下一個標簽
}else{
result = [originHtmlStr stringByReplacingOccurrencesOfString:@" " withString:@""]; // 過濾 等標簽
//result = [originHtmlStr stringByReplacingOccurrencesOf ........
}
return result;
}
- (NSString *)filterHtmlTag:(NSString *)originHtmlStr{
NSString *result = nil;
NSRange arrowTagStartRange = [originHtmlStr rangeOfString:@"<"];
if (arrowTagStartRange.location != NSNotFound) { //如果找到
NSRange arrowTagEndRange = [originHtmlStr rangeOfString:@">"];
// NSLog(@"start-> %d end-> %d", arrowTagStartRange.location, arrowTagEndRange.location);
// NSString *arrowSubString = [originHtmlStr substringWithRange:NSMakeRange(arrowTagStartRange.location, arrowTagEndRange.location - arrowTagStartRange.location)];
result = [originHtmlStr stringByReplacingCharactersInRange:NSMakeRange(arrowTagStartRange.location, arrowTagEndRange.location - arrowTagStartRange.location + 1) withString:@""];
// NSLog(@"Result--->%@", result);
return [self filterHtmlTag:result]; //遞歸,過濾下一個標簽
}else{
result = [originHtmlStr stringByReplacingOccurrencesOfString:@" " withString:@""]; // 過濾 等標簽
//result = [originHtmlStr stringByReplacingOccurrencesOf ........
}
return result;
}