String:
记录一个子串在整串中出现的次数:
package package_name; public class exp { public static void main (String[] args) { String str = "abcssssasasasnddbiweabcabcahabcccccabqwertadabc"; String key = "abc"; int res = getCounts(str, key); System.out.println(res); } public static int getCounts(String str, String key) { int count = 0; int index = 0; if(!str.contains(key)) { return count; } while((index = str.indexOf(key) )!= -1) { str = str.substring(index + key.length()); count++; } return count; } }