记录一个字符串在整串中出现的次数
public class Example08 {
/**
* @param 记录一个字符串在整串中出现的次数
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String Str="asfwiadkfasdas";
String key="a";
int count=apple(Str,key);
System.out.println("count:"+count);
}
public static int apple(String Str,String key){
int count=0;
if(Str.indexOf("key")==0){
return count;
}
int index=0;//
while (Str.indexOf(key)>-1){//证明有key
index=Str.indexOf(key);
Str=Str.substring(index+key.length());//截取后面的数字
count++;
}
return count;
}
}