How to split text with HTML tags to array

半城伤御伤魂 提交于 2019-12-07 11:22:11

问题


I have very simple text with HTML (only <b> tag) e.g.

Lorem Ipsum is <b>simply dummy</b> text of the printing and <b>typesetting industry</b>

I would like to split the text to array like this:

[0] - Lorem Ipsum is 
[1] - <b>simply dummy</b>
[2] - text of the printing and
[3] - <b>typesetting industry</b>

The text inside HTML tag must be separated from another text. Is there any simple solution for it?

Thank you


回答1:


You may achieve this using following code

string value = @"Lorem Ipsum is <b>simply dummy</b> text of the printing and <b>typesetting industry</b>";
var parts = Regex.Split(value, @"(<b>[\s\S]+?<\/b>)").Where(l => l != string.Empty).ToArray();



回答2:


I just wrote this, tested it and it works. It's a bit ugly but it works hahah

    public string[] getHtmlSplitted(String text)
    {
        var list = new List<string>();
        var pattern = "(<b>|</b>)";
        var isInTag = false;            
        var inTagValue = String.Empty;

        foreach (var subStr in Regex.Split(text, pattern))
        {
            if (subStr.Equals("<b>"))
            {
                isInTag = true;
                continue;
            }
            else if (subStr.Equals("</b>"))
            {
                isInTag = false;
                list.Add(String.Format("<b>{0}</b>", inTagValue));
                continue;
            }

            if (isInTag)
            {
                inTagValue = subStr;
                continue;
            }

            list.Add(subStr);

        }
        return list.ToArray();
    }


来源:https://stackoverflow.com/questions/30455489/how-to-split-text-with-html-tags-to-array

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