计算字符串中子串出现的次数
package com.xijiaopractice;
public class Test49 {
/*
* 计算字符串中子串出现的次数
*/
public static void main(String[] args) {
String str1="aaa";
String str2="aaaa";
System.out.println(subCount(str1, str2));
}
public static int subCount(String str,String subStr){
int count=0;
if(str.length()>=subStr.length()){
for (int i = 0; i < str.length()-subStr.length()+1; i++) {
if(str.substring(i, subStr.length()+i).equals(subStr)){
count++;
}
}
}else{
return -1;
}
return count;
}
}