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

Answer by Tushar Shukla for How to count string occurrence in string?

$
0
0

Now this is a very old thread i've come across but as many have pushed their answer's, here is mine in a hope to help someone with this simple code.

var search_value = "This is a dummy sentence!";var letter = 'a'; /*Can take any letter, have put in a var if anyone wants to use this variable dynamically*/letter = letter && "string" === typeof letter ? letter : "";var count;for (var i = count = 0; i < search_value.length; count += (search_value[i++] == letter));console.log(count);

I'm not sure if it is the fastest solution but i preferred it for simplicity and for not using regex (i just don't like using them!)


Viewing all articles
Browse latest Browse all 43

Trending Articles



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