JavaScript 计算一个短字符串在长字符串中出现的次数,多用点String对象来写
计算小串在大串中出现的次数varmaxStr="woaijavazhendeaijavawojiuyaoxuejava";varminStr="java";...
计算小串在大串中出现的次数
var maxStr="woaijavazhendeaijavawojiuyaoxuejava";
var minStr="java"; 展开
var maxStr="woaijavazhendeaijavawojiuyaoxuejava";
var minStr="java"; 展开
1个回答
展开全部
var maxStr = "woaijavazhendeaijavawojiuyaoxuejava";
var minStr = "java";
function count(str, sub){
var ret = 0, p;
if(str.length === 0 || sub.length === 0){
return 0;
}
p = str.indexOf(sub);
while(p >= 0){
ret++;
p = str.indexOf(sub, p + sub.length);
}
return ret;
}
alert(count(maxStr, minStr));
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询