首页 > Windows开发 > 详细

C#去除HTML标签

时间:2015-05-12 13:10:52      阅读:156      评论:0      收藏:0      [点我收藏+]

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

 /// <summary>
/// C#去除HTML标签
 /// </summary>
 /// <param name="html">带有html标签的文本</param>
 /// <param name="length">截取长度</param>
 /// <returns></returns>
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参数可以根据传入值取固定长度的值。用于生成文章摘要比较方便。

C#去除HTML标签

原文:http://www.cnblogs.com/weixing/p/4496753.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!