替换< ol> &安培; <李>来自iOS 6中编号的html代码中的标签 [英] Replace <ol> & <li> tags from html code with number in iOS 6

查看:89
本文介绍了替换< ol> &安培; <李>来自iOS 6中编号的html代码中的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器获取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


  1. 咖啡


  2. 牛奶


  1. Coffee
  2. Tea
  3. 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; 
}

这篇关于替换&lt; ol&gt; &安培; &LT;李&GT;来自iOS 6中编号的html代码中的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆