C#去除HTML标签

耗尽温柔 提交于 2020-01-29 05:13:01
本文转载自:https://www.cnblogs.com/youring2/archive/2013/04/03/2997826.html 作者:youring2 转载请注明该声明。

在做网站的时候,用到了去除html标签的问题,用正则匹配到html标签,然后replace即可。

public static string ReplaceHtmlTag(string html, int length = 0)
{
    string strText = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+>", "");
    strText = System.Text.RegularExpressions.Regex.Replace(strText, "&[^;]+;", "");

    if (length > 0  && strText.Length > length)
        return strText.Substring(0, length);

    return strText;
}

这个方法可以实现去除html标签的功能。

Length参数可以根据传入值取固定长度的值。用于生成文章摘要比较方便。

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