public class NumOfChar{
//ch为需要统计的字符,也可以是一个字符串
public static int getNum(String origin,String ch){
int chLength = ch.length();
if(origin.length()==0) return 0;
if(chLength==0) return -1; //如果统计的字符为空(即"",不是指空格)返回-1;
int index = 0;
int count = 0;
while(origin.indexOf(ch,index)!=-1){
++count;
index = origin.indexOf(ch,index) + chLength;
}
return count;
}
public static void main(String[] args){
System.out.println(getNum("I like beer very much","k"));
}
}