如何在iOS中将html字符串转换为普通字符串?

时间:2015-08-02 06:37:16

标签: html ios nsstring

我要问一个简单而基本的问题,不要因此而杀了我。

我想将HTML字符串解码为普通字符串。

我试过了:

 NSString*  testString =[htmlString  stringByConvertingHTMLToPlainText];
 - (NSString *)stringByStrippingTags;
 - (NSString *)stringWithNewLinesAsBRs;
 - (NSString *)stringByRemovingNewLinesAndWhitespace;
 - (NSString *)stringByDecodingHTMLEntities;
 - (NSString *)stringByEncodingHTMLEntities;

但这些都不在现在。所以帮我删除" '"和" &#8211"等,并将html转换为普通字符串。

1 个答案:

答案 0 :(得分:-1)

试试这段代码..

  • (NSString *)stringByStrippingHTML:(NSString *)inputString { NSMutableString * outString;

    if(inputString) {     if([inputString length]> 0)     {         NSRange r;

        inputString = [inputString stringByReplacingOccurrencesOfString:@"</div>" withString:@"\n\n"];
        inputString = [inputString stringByReplacingOccurrencesOfString:@"</DIV>" withString:@"\n\n"];
        inputString = [inputString stringByReplacingOccurrencesOfString:@"<br/>" withString:@"\n"];
        inputString = [inputString stringByReplacingOccurrencesOfString:@"<br />" withString:@"\n"];
        inputString = [inputString stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
        inputString = [inputString stringByReplacingOccurrencesOfString:@"<BR>" withString:@"\n"];
        inputString = [inputString stringByReplacingOccurrencesOfString:@"<BR />" withString:@"\n"];
        inputString = [inputString stringByReplacingOccurrencesOfString:@"<BR/>" withString:@"\n"];
    
        outString = [[NSMutableString alloc] initWithString:inputString];
        while ((r = [outString rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
        {
            [outString deleteCharactersInRange:r];
        }
        NSString *finalString = outString;
        finalString =[finalString stringByDecodingHTMLEntities];
        return finalString;
    }
    

    } return outString; }