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;
            }
  • 相关阅读:
    索引总结篇
    数据库的安全管理
    数据库备份对日志文件的影响
    数据文件与日志文件读取机制
    更新操作所带来的影响
    页拆分-产生碎片
    你不可不知的T-SQL执行顺序
    实用T-SQL收集
    Left Join的神奇效果
    我对数据库索引的理解
  • 原文地址:https://www.cnblogs.com/wangyinlon/p/12176222.html
Copyright © 2011-2022 走看看