/// <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 ( "?" ) ); }
新聞熱點
疑難解答