牛骨文教育服务平台(让学习变的简单)
博文笔记

统计一个字符串中某个字串出现的次数

创建时间:2017-04-09 投稿人: 浏览次数:271

//a为字符串 b为字串

方法一:

public static int hit(String a,String b){
  if(a.length()<b.length()){
   return 0;
  }
  char[]a_t=a.toCharArray();
  int count= 0;
  for (int i = 0; i < a.length(); i++) {
   StringBuffer buffer=new StringBuffer();
   for(int j=i;j<a.length()&&j-i<b.length();j++){
    buffer.append(a_t[j]);
   }
   System.out.println("buffer="+buffer.toString());
   if(buffer.toString().equals(b)){
    count ++;
   }
  }
  return count;
 }

方法二:调用API中的indexof方法

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。