What is the simplest way to count the number of occurrences of a specific character in a string?
That is, I need to write a function, countTheCharacters(), so that
str = "the little red hen"
count = countTheCharacters(str,"e") ' Count should equal 4
count = countTheCharacters(str,"t") ' Count should equal 3
Best Answer
The most straightforward is to simply loop through the characters in the string:
Usage:
Another approach that is almost as effective and gives shorter code is to use LINQ extension methods: