问题
I am displaying html data in webView (lot of content).Now i want to remove all the tags and trim the content to only 250 charecters and display in my Web-view.
Thanks In Advance....
回答1:
Included this function in the class.
in .h
- (NSString *)stringByStrippingHTML:(NSString *)inputString;
in .m
- (NSString *)stringByStrippingHTML:(NSString *)inputString
{
NSMutableString *outString;
if (inputString)
{
outString = [[NSMutableString alloc] initWithString:inputString];
if ([inputString length] > 0)
{
NSRange r;
while ((r = [outString rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
{
[outString deleteCharactersInRange:r];
}
}
}
return outString;
}
the call
NSString *plainString = [self stringByStrippingHTML:inputHTMLString ];
NSString *rangedString = [plainString substringToIndex:249]; //0 to 249 makes it 250 characters
来源:https://stackoverflow.com/questions/12894955/how-to-remove-html-tags-and-trim-the-characters-to-250-in-objective-c