我有以下方法可以从字符串中删除 html 标签和不需要的字符:
-(NSString *) stringByStrippingHTML: (NSString*) s {
NSRange r;
while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
s = [s stringByReplacingCharactersInRange:r withString:@""];
while ((r = [s rangeOfString:@"’" options:NSRegularExpressionSearch]).location != NSNotFound)
s = [s stringByReplacingCharactersInRange:r withString:@"\'"];
while ((r = [s rangeOfString:@"”" options:NSRegularExpressionSearch]).location != NSNotFound)
s = [s stringByReplacingCharactersInRange:r withString:@"\""];
while ((r = [s rangeOfString:@"“" options:NSRegularExpressionSearch]).location != NSNotFound)
s = [s stringByReplacingCharactersInRange:r withString:@"\""];
while ((r = [s rangeOfString:@"…" options:NSRegularExpressionSearch]).location != NSNotFound)
s = [s stringByReplacingCharactersInRange:r withString:@"..."];
while ((r = [s rangeOfString:@"–" options:NSRegularExpressionSearch]).location != NSNotFound)
s = [s stringByReplacingCharactersInRange:r withString:@"-"];
return s;
}
我有太多的 while 案例,如何以更优雅的方式完成?