// 统计某个字符在一个字符串中出现的次数
/**
indexOf() 方法 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
substring() 方法 从字符串中提取一些字符
**/
public static void main(String[] args) {
String a = "aaaaaabcdefghijkmlnopqrstuvwxyz";
function(a, "a");
}
public static void function(String st,String M) {
int count = 0;
while(st.indexOf(M)>=0) {
st=st.substring(st.indexOf(M)+M.length());
count++;
}
System.out.println("指定字符串在原字符串中出现:"+count+"次");
}
我觉的这个方法可以很好的解决这个问题.所以分享给大家.