Simulating an exact match lookup in Visual Studio
Public enum Enumdatatype { chinese = 0, english = 1, number = 2, special character = 3, Chinese with special character = 4, english with number = 5, }
public static class Charextend {//<summary> Get the character type///</summary>//<param name= "item" ></param>//<returns>< ;/returns> public static Enumdatatype Getdatatype (this char item) {//Range (0X4E00~0X9FFF) converted to int ( ch-from~ch-end) int chfrom = Convert.ToInt32 ("4e00", 16); int chend = Convert.ToInt32 ("9fff", 16); if (item >= ' 0 ' && item <= ' 9 ') return enumdatatype. number; else if (item >= ' A ' && item <= ' z ') return enumdatatype. English; else {int code = CHAR.CONVERTTOUTF32 (item. ToString (), 0); If (Code >= chfrom && Code <= chend) return enumdatatype. Chinese; else return enumdatatype. special characters; } } }
public static class Boolextend {public static bool IsTrue (this bool bl) { return bl==true; } public static bool IsFalse (this bool bl) { return BL = = false;} }
<summary>///Find string STRB in Stra (simulate Visual Studio Lookup)///</summary>//<param NA Me= "stra" > searched string </param>//<param name= "StrB" > String to find </param>//<param name= "Fu Llmatched "> whether the whole word match (true/false) </param>//<returns>int value, did not find this return -1</returns> public Stati c int IndexOf (string stra, String StrB, bool fullmatched) {/* * If you are looking for a Chinese, special symbol, the exact match and the non-exact matching knot If you want to find the English, the number, you need to do special processing (the exact match format "other" "English/Digital" "other" * that also need to match before and after the character type must be different from the type to find * *///If you want to check that the string length is greater than the searched string, then return directly-1 if (Strb.length > Stra.length) re turn-1; enumdatatype[] types = new enumdatatype[2]; if (Fullmatched.istrue ()) {#region myregion if ((Strb[0]. Getdatatype () = = Enumdatatype. Chinese | | Strb[0]. Getdatatype () = = Enumdatatype. Special characters)) {Types[0] = Enumdatatype. Chinese with special characters; } else {types[0] = Enumdatatype. English and numerals; } if ((Strb[strb.length-1]. Getdatatype () = = Enumdatatype. Chinese | | STRB[STRB.LENGTH-1]. Getdatatype () = = Enumdatatype. Special characters) {types[1] = Enumdatatype. Chinese with special characters; } else {types[1] = Enumdatatype. English and numerals; } #endregion} int index =-1; if (Stra.length > 1) {for (int i = 0; I <= stra.length-strb.length; i++) {//each time the equal length string in straight is compared with the StrB, determine if the Equals if (Stra.substring (i, strb.length) = = StrB) {//Not whole word match if (Fullmatched.isfalse ()) {//STRB The first horse found in StraThe starting index of the match is I index = i; Break } else//is the whole word match {//judgment variable i is start or end if (i > 0 && i < stra.length-strb.length) { #region Myregion//matches before and after the first character is not a letter and a number char start = Stra . Substring (i-1, 1) [0]; Char end = stra.substring (i + strb.length, 1) [0]; Enumdatatype Starttype=start. Getdatatype (); Enumdatatype Endtype = end. Getdatatype (); if (types[0] = = Enumdatatype. Chinese with special characters) {} else if (Types[0] = = Enumdatatype. English with numbers && startType! = enumdatatype. English && StartType! = enumdatatype. Numbers) {} else {con Tinue; } if (types[1] = = Enumdatatype. Chinese with special characters) {} else if (types[1] = = Enumdatatype. English with digital && endtype! = enumdatatype. English && endtype! = Enumdat Atype. Number) {} else { Continue }//After customs clearance, index = i;//is the whole word match Break #endregion} else if (i = = 0)//If you are starting { #region Myregion if (i + strb.length >= stra.length) {index = i;//is the whole word match break; } else { Char end = stra.substring (i + strb.length, 1) [0]; Enumdatatype Endtype = end. Getdatatype (); if (types[1] = = Enumdatatype. Chinese with special characters) {} else if (t YPES[1] = = Enumdatatype. English with numbers && endtype! = enumdatatype. English && Endtype! = enumdatatype. Numbers) {} else { Continue } index = i;//is the whole word match break; } #endregion} else if (i = = stra.length-strb.length)//IF Is the end {#region myregion char start = Stra.substring (i-1, 1) [0]; Enumdatatype StartType = start. Getdatatype (); if (types[0] = = Enumdatatype. Chinese with special characters) {} else if (Types[0] = = Enumdatatype. English with numbers && startType! = enumdatatype. English && StartType! = enumdatatype. Numbers) {} else {Co Ntinue; } index = i;//is the whole word match break; #endregion}}} }} else {if (stra = = StrB) index = 0; } return index; }
static void Main (string[] args) { testindexof (); Console.read (); } static void Testindexof () { string go = String. Empty; do { Console.WriteLine ("Enter the string to be searched for"); String stra = Console.ReadLine (); Console.WriteLine ("Please enter a string to find"); String StrB = Console.ReadLine (); Console.WriteLine ("Do you want to match the whole word (y/n)?" "); String matched = Console.ReadLine (). ToLower (); Console.WriteLine ("Built-in function returns the index value:" + ZJZCommon.Utility.IndexOf (Stra, StrB)); Console.WriteLine ("The index value returned by the Custom function:" + ZJZCommon.Utility.IndexOf (stra, strB, matched = = "Y"? true:false)); Console.WriteLine ("Do you want to continue (y/n)?" "); Go = Console.ReadLine (). ToLower (); } while (go = = "Y"); }
Reprint Please specify source: http://www.cnblogs.com/jzblogs/p/5670397.html
Simulating an exact match lookup in Visual Studio