@fivem-ts/shared
Version:
FiveM Typescript wrapper shared part
30 lines (29 loc) • 864 B
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.countSubstringOccurrences = countSubstringOccurrences;
/**
* Counts the occurrences of a substring within a string.
*
* @example
* ```ts
* const result = countSubstringOccurrences("hello hello world", "hello");
* console.log(result); // 2
* ```
*
* @param {string} input - The string to search within.
* @param {string} substring - The substring to count occurrences of.
*
* @return {number} The number of times the substring occurs in the input string.
*/
function countSubstringOccurrences(input, substring) {
if (substring.length === 0) {
return 0;
}
let count = 0;
let position = input.indexOf(substring);
while (position !== -1) {
count++;
position = input.indexOf(substring, position + 1);
}
return count;
}
;