package day_7_19;
//求一个子串在整串出现的次数?
public class SubStringDemo {
public static void main(String[] args) {
String str = "abcddbcksaddwaddaddaddddsdasdaddavavaaaddaaaaddsvdd";
String str2 = "dd";
getNum(str,str2);
}
private static void getNum(String str, String str2) {
int count=0;
int len = str2.length();
int n =0;
int i=0;
while(i<str.length()-len){
i= str.indexOf(str2, n);
n=i+len;
count++;
/* int i=str.indexOf(str2, 0);
int j =str.indexOf(str2, i+len);
int m = str.indexOf(str2, j+len);*/
}
System.out.println(count);
}
} |