——-Android Training, Java training, look forward to communicating with you! ———-
/ * Gets the number of occurrences of a string in another string. For example: "KK" in "ABKKCDKKEFKKSKK" the number of times analysis function: 1 function Result: Returns the number of times the string 1 appears in string 2------------The return value type is Int2 there is no unknown variable: there are two. First, string 1; second, String 2. --How the parameter type (String str1,string str2) implements the function: the number of times that "KK" appears in "ABKKCDKKEFKKSKK". First, manually simulate the implementation of the function: in the long string from left to right traversal find short string "KK", Find a "KK", let the counter plus 1. In the simulation process, used: 1 counter 2 accesses the contents of the string 3 the script to access the string comments: The above simulation is too simple, not good to write programs ... Take a look at teacher Bi's ideas: 1 Gets the position of the first occurrence in the string according to "KK" index; if the "KK" is not found, then the second step is done. The length of the 2 "KK" plus index is the next substring that needs to be looked up; go to step three. 3 According to the results of the second step to get the substring; go to the fourth step. 4 return to the first step. So to achieve this function requires: 1 Define a counter count2 get "KK" in the string where it appears index3 a loop, the loop condition is not found "KK" on the end * / class getsubstringcountdemo { Public Static voidMain (string[] args) {//The string being accessedString str1 ="ABKKCDKKEFKKSKK";//keyword stringString key ="KK";//Get Key number of occurrences in str1 int Count= Getsubstringcount (Str1,key);//Mode 2 get Key number of occurrences in str1 intCount_2 = Getsubstringcount_2 (Str1,key);//Mode 3: Use the Split method to obtain intCount_3 = Getsubstringcount_3 (Str1,key);//Print resultsSystem.out.println ("Count:"+Count); System.out.println ("Count_2:"+count_2); System.out.println ("Count_3:"+count_3); }//Gets the number of occurrences of key in the string str Public Static intGetsubstringcount (StringStr, String key) {//define counters int Count=0;//Define where key first appears in Str int Index=Str. indexOf (key);//method int indexOf (int ch) //Use a while loop to find when "KK" ends in a string that does not exist while(Index!=-1) {Count++;Str=Str. SUBSTRING (Index+key.length ());//Using this method string substring (begin); Index=Str. indexOf (key);//Gets the position where key first appears in the substring}return Count; }//Below is given in mode 2, mainly using the method int indexOf (String str,int fromIndex) Public Static intGetsubstringcount_2 (StringStr, String key) {//define counters int Count=0;//Define where key first appears in Str int Index=Str. indexOf (key);//method int indexOf (int ch) //Use a while loop to find when "KK" ends in a string that does not exist while(Index!=-1) {Count++;Index=Index+ Key.length ();//New start bit Index=Str. IndexOf (Key,Index);//Use method int indexOf (String str,int fromIndex)}return Count; }//below gives a wrong way, called mode 3 //Use the Split method. String[] Split (regex); Public Static intGetsubstringcount_3 (StringStr, String key) {int Count=Str. Split (key). length;//Note that it is not length (), because this is the size of the get string[] array return Count; }/ * Using mode 3 is correct in the subject, but there is a problem when there is no key at the end of the string. This is because after cutting the "ABKKCDKKEFKKSK": the Ab,cd,ef,sk is obtained. This time the string array length is 5, but "KK" only appears 3 times, so this method is not advocated! */}
Dark Horse Programmer _ Diary 24_ string Get count exercise