统计小字符串在大字符串中出现的次数

public class StringDemo {

public static void main(String[] args) {

String bigString = "javajava I love java l like jajavava I enjoy javajavajava";

String smallString = "java";
int count=getCount(bigString,smallString);
System.out.println("小字符串在大字符串中出现的次数:"+count);


}
public static int getCount(String bigString,String smallString){


int count = 0;
int index;

while ((index=bigString.indexOf(smallString))!=-1) {

count++;
bigString = bigString.substring(index + smallString.length());
}
return count;
  }

}

posted on 2017-07-17 23:08  程序员的那些事  阅读(548)  评论(0编辑  收藏  举报