C#去除HTML標籤

C#去除HTML標籤
在做網站的時候,用到了去除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參數可以根據傳入值取固定長度的值。用於生成文章摘要比較方便。

來源:https://www.cnblogs.com/youring2/archive/2013/04/03/2997826.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章