MCPcopy Create free account
hub / github.com/TheAlgorithms/JavaScript / countSubstrings

Function countSubstrings

String/CountSubstrings.js:11–27  ·  view source on GitHub ↗
(str, substring)

Source from the content-addressed store, hash-verified

9 */
10
11const countSubstrings = (str, substring) => {
12 if (typeof str !== 'string' || typeof substring !== 'string') {
13 throw new TypeError('Argument should be string')
14 }
15
16 if (substring.length === 0) return str.length + 1
17
18 let count = 0
19 let position = str.indexOf(substring)
20
21 while (position > -1) {
22 count++
23 position = str.indexOf(substring, position + 1)
24 }
25
26 return count
27}
28
29export { countSubstrings }

Callers 1

Calls 1

indexOfMethod · 0.45

Tested by

no test coverage detected