统计小字符串在大字符串中出现的次数
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;
}
}
知识改变命运,代码改变生活。