替换< ol> &安培; <李>来自iOS 6中编号的html代码中的标签 [英] Replace <ol> & <li> tags from html code with number in iOS 6
问题描述
我从服务器获取html代码&我想在UITextView中显示它。我需要解析html&在UITextView中显示纯文本。我使用下面的代码来解析这个 post
I am getting html code from server & I want to display it in UITextView. I need to parse the html & show plain text in UITextView. I am using below code to parse html from this post
现在我需要将 li
标记替换为实际数字。我怎样才能做到这一点 ?
例如输入
Now I need to replace the li
tag with the actual numbers . How can I do that ?
e.g. Input
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
输出应该是
Output should be
- 咖啡
- 茶
- 牛奶
- Coffee
- Tea
- Milk
<$ c
推荐答案
将您的Function替换为以下内容并传递您发布的HTML字符串片段。 c> NSString * str = @< ol>
< li>咖啡< / li>
< li>茶< / li>
< li>牛奶< / li>
< / ol>;
[self stringByStrippingHTML:str];
NSString *str = @"<ol>"
"<li>Coffee</li>"
"<li>Tea</li>"
"<li>Milk</li>"
"</ol>";
[self stringByStrippingHTML:str];
函数替换字符串和生成期望输出
Function to replace string and Generate Desire output
- (NSString *)stringByStrippingHTML:(NSString *)inputString
{
NSMutableString *outString;
if (inputString)
{
outString = [[NSMutableString alloc] initWithString:inputString];
if ([inputString length] > 0)
{
NSRange r;
int index = 1;
while ((r = [outString rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
{
if([[outString substringWithRange:r] isEqualToString:@"<li>"])
{
outString = [[outString stringByReplacingCharactersInRange:r withString:[NSString stringWithFormat:@"\n %d. ",index++]] mutableCopy];
}
else
[outString deleteCharactersInRange:r];
}
}
}
return outString;
}
这篇关于替换< ol> &安培; <李>来自iOS 6中编号的html代码中的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!