Quantcast
Channel: How to count string occurrence in string? - Stack Overflow
Viewing all articles
Browse latest Browse all 43

Answer by H S W for How to count string occurrence in string?

$
0
0

A simple way would be to split the string on the required word, the word for which we want to calculate the number of occurences, and subtract 1 from the number of parts:

function checkOccurences(string, word) {      return string.split(word).length - 1;}const text="Let us see. see above, see below, see forward, see backward, see left, see right until we will be right"; const count=countOccurences(text,"see "); // 2

Viewing all articles
Browse latest Browse all 43


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>