asp.net文章截取摘要用省略号表示

正则简易版去除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, "&[^;]+;", "");

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

    return strText;
}

先去富文本中的所有html标签,然后再截取

 #region
        /// <summary>
        /// 移除富文本中的html标签
        /// </summary>
        /// <param name="htmlstring">字符串</param>
        /// <returns></returns>
        public  string RemoveHtml(string htmlstring)
        {

            //删除脚本

            htmlstring = Regex.Replace(htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);

            //删除HTML

            htmlstring = Regex.Replace(htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);

            htmlstring = Regex.Replace(htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);

            htmlstring = Regex.Replace(htmlstring, @"-->", "", RegexOptions.IgnoreCase);

            htmlstring = Regex.Replace(htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);



            htmlstring = Regex.Replace(htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);

            htmlstring = Regex.Replace(htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);

            htmlstring = Regex.Replace(htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);

            htmlstring = Regex.Replace(htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);

            htmlstring = Regex.Replace(htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);

            htmlstring = Regex.Replace(htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);

            htmlstring = Regex.Replace(htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);

            htmlstring = Regex.Replace(htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);

            htmlstring = Regex.Replace(htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);

            htmlstring = Regex.Replace(htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);



            htmlstring = htmlstring.Replace("<", "");

            htmlstring= htmlstring.Replace(">", "");

            htmlstring= htmlstring.Replace("\r\n", "");

            htmlstring = HttpContext.Current.Server.HtmlEncode(htmlstring).Trim();



            return htmlstring;

        }

        /// <summary>
        /// 字符串截取函数
        /// </summary>
        /// <param name="inputString">字符串</param>
        /// <param name="len">字节长度</param>
        /// <returns></returns>
        public string CutString(string inputString, int len)
        {
            var ascii = new ASCIIEncoding();
            int tempLen = 0;
            string tempString = "";
            byte[] s = ascii.GetBytes(inputString);
            for (int i = 0; i < s.Length; i++)
            {
                if ((int)s[i] == 63)
                {
                    tempLen += 2;
                }
                else
                {
                    tempLen += 1;
                }

                try
                {
                    tempString += inputString.Substring(i, 1);
                }
                catch
                {
                    break;
                }

                if (tempLen > len)
                    break;
            }
            //如果截过则加上半个省略号 
            byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
            if (mybyte.Length > len)
                tempString += "…";


            return tempString;
        }
        #endregion
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值