zoukankan      html  css  js  c++  java
  • 网站博客截取简介

          public static string HtmlCutString(string str, int length, string character = "")
            {
                str = System.Web.HttpUtility.HtmlDecode(str.Replace("
    ", ""));
                int len = 0, tagIndex = 0, strlength = str.Length;
                Regex objRegExp = new Regex("(<.+?>)", RegexOptions.IgnoreCase);
                MatchCollection matches = objRegExp.Matches(str);
                for (int i = 0; i < strlength; i++)
                {
                    if (matches.Count > tagIndex && i == matches[tagIndex].Index)
                    {
                        i += matches[tagIndex].Result("$1").Length - 1;
                        tagIndex++;
                    }
                    else
                    {
                        if (System.Text.Encoding.Default.GetBytes(str.Substring(i, 1)).Length > 1)
                            len += 2;
                        else
                            len += 1;
                    }
                    if (len >= length)
                    {
                        string other = "";
                        while (matches.Count > tagIndex)
                        {
                            other += matches[tagIndex].Value;
                            tagIndex++;
                        }
                        int otherlength = other.Length;
                        objRegExp = new Regex(@"<s*(.+?)[^>]*?><s*/s*1s*>|<s*(.+?).*?/s*>", RegexOptions.IgnoreCase);
                        while (objRegExp.Matches(other).Count > 0)
                        {
                            other = objRegExp.Replace(other, "");
                        }
                        other = new Regex(@"<s*[^/]+?>", RegexOptions.IgnoreCase).Replace(other, "");
                        if (i + otherlength + 1 < strlength)
                            str = str.Substring(0, i + 1 - character.Length) + character + other;
                        else
                            str = str.Substring(0, i + 1) + other;
                        break;
                    }
                }
                return str;
            }
  • 相关阅读:
    springboot添加邮件发送及压缩功能
    springboot添加多数据源连接池并配置Mybatis
    SpringMVC+Mybatis初尝试
    个人课程总结
    第十六周学习总结
    第十五周学习总结
    第二阶段冲刺九
    第二阶段冲刺八
    第二阶段冲刺七
    搜狗拼音输入法使用评价
  • 原文地址:https://www.cnblogs.com/wangyinlon/p/12176222.html
Copyright © 2011-2022 走看看