编写一个方法,输出在一个字符串中,指定字符串出现的次数。
public class yanzhuo {
public static void method(String a, String b) {
int t = 0;
for (int i = 0; i <= a.length() - b.length(); i++)
if (b.regionMatches(0, a, i, b.length())) {
i += b.length() - 1;
t++;
}
System.out.println("字符串" + a + "中" + b + "出现了" + t + "次");
}
public static void main(String[] args) {
method("dsadsdsasss", "ss");
}
}
运行结果: