网站博客截取简介
Posted wangyinlon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了网站博客截取简介相关的知识,希望对你有一定的参考价值。
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; }
以上是关于网站博客截取简介的主要内容,如果未能解决你的问题,请参考以下文章