我有以下方法從一個字符串中刪除HTML標記和不想要的字符:當案件刪除HTML標籤和不想要的字符中的iOS
-(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;
}
我有太多的,這怎麼能更完成優雅的時尚?
我可以使用別人所做的,但我想知道我現在可以做些什麼。感謝您的答覆! – Carmichael
我會將您的每個案例添加到自定義字符集中,然後根據該字符集剝離字符串;而不是每種情況都有一個while循環。 – Tyler
@TylerAndFriends字符集在這種情況下不起作用,因爲應該被替換的字符串包含具有多個字符的序列。如果您想要替換所有出現的&#8211,您不希望替換所有出現的&&,##,8#等,但是這些字符以特定順序出現的所有事件。 – omz