统计某字符串中某子串出现的次数

class StringDemo1
{
public static void main(String[] args) {
int n = getString("abcdnfgabchedabckkfdnabct","abc");
System.out.println("字符串出现的次数为:"+n);
}
public static int getString(String str,String str1){
   int count=0;
int index=0;
while(true){
index = str.indexOf(str1,index);
if(index==-1){
break;
}
index = index + str1.length();
count++;
}
return count;
}
}

你可能感兴趣的:(Java编程)