标签:
/// <summary> /// 涂聚文 /// 2011 捷为工作室 /// 缔友计算机信息技术有限公司 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { this.Page.Title = "字符串操作"; StringBuilder search =new StringBuilder(); //查找以a字母至多5个字符放在匹配字边界 string text = @"This comprehensive compendium provides a broad and thorough investigation of all aspects of programming with asp.net. entity revised and updated for the 3.5 release of .net, this book will give you the information you need to master asp.net and build a dynamic, successful,enterprsie web application."; string pattern = @"\ba";//\b字边界,\B 不是字边界的位界 这是特殊字符或转义序列 MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase); //WriteMatches(text, matches); Response.Write("Original text was:" + text + "<br/>"); Response.Write("No.of matches:" + matches.Count.ToString() + "<br/>"); foreach (Match nextMatch in matches) { int Index = nextMatch.Index; string result = nextMatch.ToString(); int charsBefore = (Index < 5) ? Index : 5; int formEnd = text.Length - Index - result.Length; int charsAfter = (formEnd < 5) ? formEnd : 5; int charsToDisplay = charsBefore + charsAfter + result.Length; //Console.WriteLine("index:{0},\t string:{i},\t {2}", Index, result, text.Substring(Index - charsBefore, charsToDisplay)); Response.Write("Index:" + Index); Response.Write(", String:" + result); Response.Write(",..." + text.Substring(Index - charsBefore, charsToDisplay).Replace("a", "<font color=red>a</font>") + "...<br/>"); search.Append(text.Substring(Index - charsBefore, charsToDisplay).Replace("a", "<font color=red>a</font>")); } #region 得出结果 /* No.of matches:10 Index:39, String:a,ides a broa Index:47, String:a,road and th Index:77, String:a,n of all as Index:81, String:a, all aspect Index:109, String:a,with asp.ne Index:133, String:a,ised and up Index:233, String:a,ster asp.ne Index:241, String:a,.net and bu Index:251, String:a,uild a dyna Index:288, String:a, web applic */ #endregion Response.Write(search.ToString()); Response.Write("...<br/>"); Response.Write(SeachText(text,"a",5, pattern)); } /// <summary> /// 搜索文章内容得到搜索关键字截取显示 /// </summary> /// <param name="text"></param> /// <param name="strword"></param> /// <returns></returns> private string SeachText(string text, string strword, int num, string pattern) { StringBuilder search = new StringBuilder(); //string text = @"This comprehensive compendium provides a broad and thorough investigation of all aspects of programming with asp.net. entity revised and updated for the 3.5 release of .net, this book will give you the information you need to master asp.net and build a dynamic, successful,enterprsie web application."; //string pattern = @"\ba";//\b字边界,\B 不是字边界的位界 这是特殊字符或转义序列 MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase); //Response.Write("Original text was:" + text + "<br/>"); //Response.Write("No.of matches:" + matches.Count.ToString() + "<br/>"); foreach (Match nextMatch in matches) { int Index = nextMatch.Index; string result = nextMatch.ToString(); int charsBefore = (Index < num) ? Index : num; int formEnd = text.Length - Index - result.Length; int charsAfter = (formEnd < num) ? formEnd : num; int charsToDisplay = charsBefore + charsAfter + result.Length; //Console.WriteLine("index:{0},\t string:{i},\t {2}", Index, result, text.Substring(Index - charsBefore, charsToDisplay)); //Response.Write("Index:" + Index); //Response.Write(", String:" + result); //Response.Write(",..." + text.Substring(Index - charsBefore, charsToDisplay).Replace(strword, "<font color=red>a</font>") + "...<br/>"); string reword ="<span style=‘color:#FF0000;‘>"+ strword +"</span>";// "<font color=red>" + strword + "</font>"; search.Append(text.Substring(Index - charsBefore, charsToDisplay).Replace(strword, reword)); } return search.ToString(); } /// <summary> /// 首先是前台调用:strBuilder.Append ( GetLogExtract ( LogText, WordNum, LogUrl ) ); /// </summary> /// <param name="LogText"></param> /// <param name="WordNum"></param> /// <param name="LogUrl"></param> /// <returns></returns> public string GetLogExtract ( string LogText, int WordNum, string LogUrl ) { string LogExtract=LogText; if ( LogText.IndexOf ( "#此前内容作为摘要#" )>0 ) { LogExtract = LogText.Substring ( 0, LogText.IndexOf ( "#此前内容作为摘要#" ) ) + "……<a href=\""+LogUrl+"\">阅读全文>>></a>"; } else { int Contentlen = GetStrLength ( LogText ); if ( Contentlen <= WordNum ) { LogExtract = LogText; } else { if ( LogText.LastIndexOf ( "<object" ) >0 || LogText.LastIndexOf ( "<OBJECT" ) >0 ) { if ( WordNum <100 ) { LogExtract=""; } else { LogExtract = WipeOffTableHTML ( LogText ); } } else { LogExtract = InterceptStr (WipeOffTableHTML ( LogText ), WordNum+100 ); if ( LogExtract.LastIndexOf ( "<p" )>0 && (LogExtract.Length - LogExtract.LastIndexOf ( "<p" ))<400 ) { LogExtract = LogExtract.Substring ( 0, LogExtract.LastIndexOf ( "<p" ) -1 ); } else if ( LogExtract.LastIndexOf ( "<img" )>0 && (LogExtract.Length - LogExtract.LastIndexOf ( "<img" ))<400 ) { LogExtract = LogExtract.Substring ( 0, LogExtract.LastIndexOf ( "<img" ) -1 ); } else if ( LogExtract.LastIndexOf ( "。" )>0 && (LogExtract.Length - LogExtract.LastIndexOf ( "。" ))<400 ) { LogExtract = LogExtract.Substring ( 0, LogExtract.LastIndexOf ( "。" ) ); } else if ( LogExtract.LastIndexOf ( "<br" )>0 && (LogExtract.Length - LogExtract.LastIndexOf ( "<br" ))<400 ) { LogExtract = LogExtract.Substring ( 0, LogExtract.LastIndexOf ( "<br" ) -1 ); } else if ( LogExtract.LastIndexOf ( "?" )>0 && (LogExtract.Length - LogExtract.LastIndexOf ( "?" ))<400 ) { LogExtract = LogExtract.Substring ( 0, LogExtract.LastIndexOf ( "?" ) ); } LogExtract += "……<a href=\""+LogUrl+"\">阅读全文>>></a>"; } } } return LogExtract; } /// <summary> /// 去除表格HTML标记 /// </summary> /// <param name="Htmlstring"></param> /// <returns>去掉表格HTML标记后的文本</returns> public static string WipeOffTableHTML(string Htmlstring) { //删除脚本 Htmlstring = Regex.Replace(Htmlstring, @"<[^>]*?>.*?</>", "", RegexOptions.IgnoreCase); //删除表格HTML Htmlstring = Regex.Replace(Htmlstring, @"</?table[^>]*>", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"</?tr[^>]*>", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"</?td[^>]*>", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"</?th[^>]*>", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"</?BLOCKQUOTE[^>]*>", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"</?tbody[^>]*>", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"<style[^\s]*", "", RegexOptions.IgnoreCase); return Htmlstring; } /// <summary> /// /// </summary> /// <param name="str"></param> /// <returns></returns> public static int GetStrLength(string str) { bool WINNT_CHINESE = ("中国".Length == 2); if (WINNT_CHINESE) { int L, T, C; L = str.Length; T = L; for (int i = 1; i <= L; i++) { Encoding ASCII = Encoding.ASCII; Byte[] EncodedBytes = ASCII.GetBytes(str.Substring(i - 1, 1)); C = EncodedBytes[0]; if (C < 0) C += 65536; if (C > 255) T += 1; } return T; } else { return str.Length; } } /// <summary> /// /// </summary> /// <param name="str"></param> /// <param name="length"></param> /// <returns></returns> public static string InterceptStr(string str, int length) { int x, y; str = str.Trim(); x = str.Length; y = 0; if (x >= 1) { for (int i = 1; i <= x; i++) { Encoding ASCII = Encoding.ASCII; Byte[] EncodedBytes = ASCII.GetBytes(str.Substring(i - 1, 1)); if (EncodedBytes[0] < 0 || EncodedBytes[0] > 255) { y += 2; } else { y += 1; } if (y >= length) { str = str.Substring(0, i); break; } } return str; } else { return ""; } }
标签:
原文地址:http://www.cnblogs.com/geovindu/p/4278922.html