使用此函数能返回字符串中某个字符或字符串出现的次数其中mainStr为要查找的字符串,subStr为要检查的字符串或字符
function countInstances(mainStr, subStr) { var count = 0; var offset = 0; do { offset = mainStr.indexOf(subStr, offset); if(offset != -1) { count++; offset += subStr.length; } }while(offset != -1) return count; } |
时间: 2024-12-03 12:21:05