public static void main(String[] args) {
// 定义大串
String maxString = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
// 定义小串
String minString = "java";
// 写功能实现
int count = getCount(maxString, minString);
System.out.println("Java在大串中出现了:" + count + "次");
}
/*
* 两个明确: 返回值类型:int 参数列表:两个字符串
*/
public static int getCount(String maxString, String minString) {
// 定义一个统计变量,初始化值是0
int count = 0;
int index;
//先查,赋值,判断
while((index=maxString.indexOf(minString))!=-1){
count++;
maxString = maxString.substring(index + minString.length());
}
return count;
}
} |
|