C#去除HTML标签

C#去除HTML标签
在作网站的时候,用到了去除html标签的问题,用正则匹配到html标签,而后replace便可。html

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

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

return strText;

}
这个方法能够实现去除html标签的功能。ide

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

来源:https://www.cnblogs.com/youring2/archive/2013/04/03/2997826.htmlcode

相关文章
相关标签/搜索