今天自己用 split()方法實(shí)現(xiàn)了統(tǒng)計(jì)某字符在字符串中出現(xiàn)的次數(shù),順便又百度了一下,看看有沒有其它方法,結(jié)果看到下面這段函數(shù),但其中的count++; offset += subStr.length;有點(diǎn)看不明白是什么意思,請路過的前輩解惑!function countInstances (mainStr, subStr) { var count = 0; var offset = 0; do{ offset = mainStr.indexOf(subStr, offset); // 通過indexOf獲得某字符在字符串中出現(xiàn)的位置 if( offset != -1 ) { // 如果某字符存在于字符串中 count++; offset += subStr.length; } } while ( offset != -1 ); return count;}countInstances('www.segmentfault.com', '.')// alert( countInstances('www.segmentfault.com', '.') );
關(guān)于某字符在字符串中出現(xiàn)的次數(shù)統(tǒng)計(jì),網(wǎng)上看到一段代碼其中有個(gè)地方看不懂什么意思
炎炎設(shè)計(jì)
2018-11-30 23:19:37