while ([theScanner isAtEnd] == NO) { // find start of tag [theScanner scanUpToString:@"<" intoString:NULL] ; // find end of tag [theScanner scanUpToString:@">" intoString:&text] ; // replace the found tag with a space //(you can filter multi-spaces out later if you wish) self = [self stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>", text] withString:@" "]; } returnself; }
方法二
用NSString自带的Seprated自截断方法
NSString+Jvaeyhcd.h
1 2 3 4 5 6 7
#import <Foundation/Foundation.h>
@interfaceNSString (Jvaeyhcd)
- (NSString *)removeHTML2;
@end
NSString+Jvaeyhcd.m
1 2 3 4 5 6 7 8 9 10 11 12
- (NSString *)removeHTML2 { NSArray *components = [self componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]; NSMutableArray *componentsToKeep = [NSMutableArray array]; for (int i = 0; i < [components count]; i = i + 2) { [componentsToKeep addObject:[components objectAtIndex:i]]; } NSString *plainText = [componentsToKeep componentsJoinedByString:@""]; return plainText; }