Java计算字符串中字母出现的次数

话不多说,直接上代码...........

public static void main(String[] args) {

  String str="I'm go to swimming";
  Set<String> set=new HashSet<>();
  for (int i = 0; i < str.length(); i++) {
    String s = str.substring(i, i+1);
    set.add(s);
  }

  Iterator<String> it = set.iterator();
  while(it.hasNext()){
    String sr=(String)it.next();
  int k=0;
  for (int j = 0; j < str.length(); j++) {
    if(sr.equals(str.substring(j,j+1))){
    k++;
    }
   }
  System.out.println(sr+"有"+k+"个");
  }

}

原文地址:https://www.cnblogs.com/tianwanfang/p/7977113.html