Skip to content

Instantly share code, notes, and snippets.

@alanerzhao
Last active December 19, 2015 08:49
Show Gist options
  • Save alanerzhao/5928113 to your computer and use it in GitHub Desktop.
Save alanerzhao/5928113 to your computer and use it in GitHub Desktop.
function strSearch(defaultStr,searchStr){
var arr = [],
i = 0,
num = 0;
length = defaultStr.length;
for(i; i<length; i++) {
//查找字符
if(defaultStr.indexOf(searchStr,i) != -1) {
num++;
i = defaultStr.indexOf(searchStr,i);
arr.push(i);
}
}
return {
num :"你查找的"+searchStr+"次数为:"+num,
arr : "出现的位置:"+arr
};
}
console.log(strSearch("sdasadasd","s"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment