去除Html标签

解析html最主要的就是利用正则表达式解析w3c中的html标签

    public class HtmlHelper
    {
        public static string ReplaceHtmlTag(string html, int length = 0)
        {
            string strText = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+>", "");//替换html标签
            strText = System.Text.RegularExpressions.Regex.Replace(strText, "&[^;]+;", "");//替换&gt\&lt等特殊标签
            if (length > 0 && strText.Length > length)
                return strText.Substring(0, length);
            strText = strText.Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", "");//去除换行符、制表符、空格
            return strText;
        }
    }






posted @ 2018-07-04 11:39  Jonny-Xhl  阅读(98)  评论(0)    收藏  举报