June 2003
Intermediate to advanced
800 pages
34h 20m
English
You want to count how many times a certain word or sequence of characters occurs in a string.
Call the String.IndexOf method multiple times in a loop, until you reach the end of the string.
The String class provides an IndexOf method that returns the location of the first match in a string. Fortunately, there is an overloaded version of IndexOf that enables you to specify a starting position for the search. By stepping through the string in a loop, specifying greater and greater start positions after each match, you can retrieve a list of all matches. (Another overload enables you to limit the number of characters the search will examine.)
Here’s a function that simply counts ...
Read now
Unlock full access