1.键盘录入一个大字符串,再录入一个小字符串
public static void main(String[] args) {
//两个字符串s1和s2
String s1 ="javajavavavajavajvajjvava";
String s2 = "va";
//定义计数器count
int count = 0;
//计数
for (int a = 0; a < s1.length()-4; a++) {
//if判断如果对s1截取长度为s2的字符串与s2相同,则计数器++
if (s1.substring(a, a+s2.length()).equals(s2)) {
count++;
}
}
// 输出
System.out.println(count);
}
|