Objective-C: How to replace HTML entities? [duplicate]

隐身守侯 提交于 2019-11-26 11:14:48

问题


I\'m getting text from Internet and it contains html entities (i.e. ó = ó). I want to show this text into a custom iPhone cell.

I\'ve tried to use a UIWebView into my custom cell but I prefer to use a multiline UILabel. The problem is I can\'t find any way of replacing these HTML entities.


回答1:


Check out my NSString category for HTML. Here are the methods available:

- (NSString *)stringByConvertingHTMLToPlainText;
- (NSString *)stringByDecodingHTMLEntities;
- (NSString *)stringByEncodingHTMLEntities;
- (NSString *)stringWithNewLinesAsBRs;
- (NSString *)stringByRemovingNewLinesAndWhitespace;



回答2:


Google Toolbox for Mac includes an iPhone-compatible NSString addition that will do this for you: gtm_stringByUnescapingFromHTML defined in GTMNSString+HTML.h and GTMNSString+HTML.m. If you comment out the calls to _GTMDevLog and #import "GTMDefines.h" in the .m you only need to add these two files to your project.




回答3:


You can make a method that can replace html entities with strings given by you.

+(NSString*)parseString:(NSString*)str
{
    str  = [str stringByReplacingOccurrencesOfString:@"–" withString:@"-"];
    str  = [str stringByReplacingOccurrencesOfString:@"”" withString:@"\""];          
    str  = [str stringByReplacingOccurrencesOfString:@"“" withString:@"\""];          
    str  = [str stringByReplacingOccurrencesOfString:@"ó" withString:@"o"];          
    str  = [str stringByReplacingOccurrencesOfString:@"'" withString:@"'"];                
    return str;
}

call this method to replace string by sending string as parameter.




回答4:


To expand on Matt Stevens answer (since I'm not allowed to comment yet), you don't need to comment out _GTMDevLog, as it is intentionally set up so that you can #define it yourself to whatever you want.




回答5:


Can you just use NSMutableString's replaceOccurrencesOfString:withString:options:range: method?



来源:https://stackoverflow.com/questions/2364797/objective-c-how-to-replace-html-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!