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

Solution 1:

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

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

Solution 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.

Solution 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.