任务描述:

编写一个程序,记录一个子串在整串中出现的次数,例如记录子串“nba”在整串“nbaernbatnbaynbauinbaopnba”中出现的次数,通过观察可知子串“nba”出现的次数为6.要求使用String类的常用方法来计算出现的次数。
任务目标:
学会分析“记录一个子串在整串中出现的次数”任务的实现思路
根据思路独立完成“记录一个子串在整串中出现的次数”任务的源代码编写、编译及运行。
掌握String类中常用方法的使用。
public class Demo01 {
public static void main(String[] args) {
String s= "nbaernbatynbauinbaopnba";
String key="nba";
int count=count(s,key);
System.out.println(count);
}
public static int count(String s, String key) {
int count=0;
int d=0;
while((d=s.indexOf(key,d))!=-1){
s=s.substring(d+key.length());
count++;
}
return count;
}
} 
售后响应及时
7×24小时客服热线
数据备份
更安全、更高效、更稳定
价格公道精准
项目经理精准报价不弄虚作假
合作无风险
重合同讲信誉,无效全额退款