Simulate the exact match search in Visual Studio, and the studio exact match
Simulate exact search in Visual Studio
Public enum EnumDataType {Chinese = 0, English = 1, number = 2, special character = 3, Chinese and special character = 4, English and number = 5 ,}
Public static class CharExtend {// <summary> // obtain the character type /// </summary> /// <param name = "item"> </param> /// <returns> </returns> public static EnumDataType GetDataType (this char item) {// range (0x4e00 ~ 0x9fff) 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. english; 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> /// search for the start and end indexes of strB in strA (simulate Visual Studio search) /// </summary> /// <param name = "strA"> string to be searched </param> /// <param name = "strB"> string </param> /// <param name = "fullMatched"> whether to match all characters (true/false) </param> // <returns> int value. If no value is found,-1 is returned. </returns> public static int IndexOf (string strA, string strB, bool fullMatched) is returned) {/** if you want to search for Chinese and special characters, the exact match is the same as the non-exact match result. ** if you want to search for English and numbers, special processing is required (in exact match format [others] [English/numbers] [others ]* That is, the pre-and post-match character types must be different from the type to be searched. ** // if You Want to query a string with a length greater than the searched string,-1 if (strB. length> strA. length) return-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 and special characters;} else {types [0] = EnumDataType. english and numbers;} if (strB [strB. length-1]. getDataType () = EnumDataType. chinese | strB [strB. length-1]. getDataType () = EnumDataType. special characters) {types [1] = EnumDataType. chinese and special characters;} else {types [1] = EnumDataType. english and numbers;} # endregion} int index =-1; if (strA. length> 1) {for (int I = 0; I <= strA. length-strB. length; I ++) {// compare the string with strB in strAight each time to determine whether it is equal if (strA. substring (I, strB. length) = strB) {// if (fullMatched. isFalse () {// strB found in strA The starting index of the first match is I index = I; break ;} else // match all characters {// determine whether the variable I starts or ends if (I> 0 & I <strA. length-strB. length) {# The first character before and after region MyRegion // match is not a letter or 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 and special characters) {} else if (types [0] = EnumDataType. English and numbers & startType! = EnumDataType. English & startType! = EnumDataType. number) {} else {continue;} if (types [1] = EnumDataType. chinese and special characters) {} else if (types [1] = EnumDataType. english and numbers & endType! = EnumDataType. English & endType! = EnumDataType. number) {} else {continue;} // After customs clearance, find the index = I; // match the full-text break; # endregion} else if (I = 0) // if it is the start {# region MyRegion if (I + strB. length> = strA. length) {index = I; // full match break;} else {char end = strA. substring (I + strB. length, 1) [0]; EnumDataType endType = end. getDataType (); if (types [1] = EnumDataType. chinese and special characters) {} else if (types [1] = EnumDataType. english and numbers & endType! = EnumDataType. English & endType! = EnumDataType. number) {} else {continue;} index = I; // full match break;} # endregion} else if (I = strA. length-strB. length) // if it is the end {# region MyRegion char start = strA. substring (I-1, 1) [0]; EnumDataType startType = start. getDataType (); if (types [0] = EnumDataType. chinese and special characters) {} else if (types [0] = EnumDataType. english and numbers & startType! = EnumDataType. English & startType! = EnumDataType. number) {} else {continue;} index = I; // full 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"); string strA = Console. readLine (); Console. writeLine ("Enter the string to be searched"); string strB = Console. readLine (); Console. writeLine ("is full match (Y/N )? "); String matched = Console. readLine (). toLower (); Console. writeLine ("index value returned by the built-in function:" + ZJZCommon. utility. indexOf (strA, strB); Console. writeLine ("index value returned by the custom function:" + ZJZCommon. utility. indexOf (strA, strB, matched = "y "? True: false); Console. WriteLine ("continue (Y/N )? "); Go = Console. ReadLine (). ToLower ();} while (go =" y ");}
Reprinted please indicate the source: http://www.cnblogs.com/jzblogs/p/5670397.html