1 PackageCom.it18zhang.day7;2 /**3 * Find the number of Java string occurrences4 * @authorFeigu5 *6 */7 Public classTestcount {8 9 Ten Public Static voidMain (string[] args) { OneString maxstring = "Woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun"; A //String maxstring= "Alldlla"; -String minstring = "java"; - intCount=0; the //calculate the location of the first occurrence - intindex=Maxstring.indexof (minstring); - //find the length of a string - intlen=minstring.length (); + //No-1 description contains a lookup string that can enter a while loop - while(Index!=-1){ + //Number of occurrences +1 Acount++; at //calculates the position of the next string intercept (all previous characters are removed) - intstartindex=len+index; - //get a new string after intercept -maxstring=maxstring.substring (startIndex); - //re-determine if the new string contains a string to find -index=Maxstring.indexof (minstring); in } -System.out.println (minstring+ "Number of occurrences:" +count); to } +}
javase-String Count Lookup