Csharp: search string, csharpstring
/// <Summary> /// tu juwen /// 2011 () /// </summary> /// <param name = "sender"> </param> // <param name = "e"> </param> protected void Page_Load (object sender, eventArgs e) {this. page. title = "string operation"; StringBuilder search = new StringBuilder (); // search for the string text = @ "This comprehensive compendium provides a broad and thorough investigation of all aspects of programming w with a letter up to five characters Ith asp.net. entity revised and updated for the 3.5 release. net, this book will give you the information you need to master asp.net and build a dynamic, successful, Sie sie web application. "; string pattern = @" \ ba "; // \ B word boundary, \ B is not the bit boundary of the word boundary. This is a special character or escape sequence 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 returns/* 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: ,. net and bu I Ndex: 251, String: a, uild a LS-DYNA Index: 288, String: a, web applic */# endregion Response. write (search. toString (); Response. write ("... <br/> "); Response. write (SeachText (text, "a", 5, pattern ));} /// <summary> /// search for the content of an article to obtain the search keyword. // </summary> /// <param name = "text"> </param>/ // <param name = "strword"> </param> // <returns> </returns> private string SeachText (string text, string strword, int num, strin G 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. net, this book will give you the information you need to master asp.net and build a dynamic, successful, Sie sie web application. "; // string pattern = @"\ Ba "; // \ B word boundary, \ B is not the bit boundary of the word boundary. This is a special character or escape sequence 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> // the front-end call is strBuilder. append (GetLogExtract (LogText, WordNum, LogUrl); // </summary> // <param name = "LogText"> </param> // <par Am name = "WordNum"> </param> /// <param name = "LogUrl"> </param> /// <returns> </returns> public string GetLogExtract (string LogText, int WordNum, string LogUrl) {string LogExtract = LogText; if (LogText. indexOf ("# previous content as abstract #")> 0) {LogExtract = LogText. substring (0, LogText. indexOf ("# previous content as abstract #") + "...... <A href = \ "" + LogUrl + "\"> read the full text >>></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 (" 0 & (LogExtract. length-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 + "\"> read the full text >>></a> ";}}} return LogExtract ;} /// <summary> /// remove the HTML tag of the table /// </summary> /// <param name = "Htmlstring"> </param> // <returns> remove text marked with table HTML </returns> public static string WipeOffTableHTML (string Htmlstring) {// Delete the script Htmlstring = Regex. replace (Htmlstring, @ "<[^>] *?>. *? </> "," ", RegexOptions. IgnoreCase); // Delete the table 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 = ("China ". 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 "";}}