<script> const _searchStrIndexOf = (str, target) => { // 补全代码 // 1.indexOf查找target在str中第一次出现的索引 let index = str.indexOf(target); let num = 0; while(index > -1) { // 继续向后查找str是否还有子串target index = str.indexOf(target, index+1); num++; } return num; } </script>
<script> const _searchStrIndexOf = (str, target) => { // 补全代码 var arr = str.split(target); return arr.length-1; } </script>
<script> let str = "sssssst"; let target = "ss"; let i = 0; function _rank(str, target) { if (str.indexOf(target) != -1) { str = str.slice(str.indexOf(target) + 1) i++ return _rank(str, target) } } _rank(str, target) console.log(i) </script>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> </head> <body> <script> const _searchStrIndexOf = (str, target) => { let index = -1; let len = target.length; let sum = 0; do{ index = str.indexOf(target,index+len); sum++; }while(index != -1) sum--; return sum; } </script> </body> </html>
const _searchStrIndexOf = (str, target) => { // 补全代码 return str.split(target).length - 1 }