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;
}
}
时间: 2024-10-13 03:02:25