Count the Number of Occurrences of a Character Sequence in a String (VB6+)

Category:
String Manipulation
Type:
Snippets
Difficulty:
Beginning
Author:
Tiziano Labruzzo

Version Compatibility: Visual Basic 6

More information:
At http://www.freevbcode.com/ShowCode.Asp?ID=1025, you will find a function that allows you to count the number of occurrences of a character or a substring in a string. That function is VB5 friendly and allows you to control whether or not to do a case-sensitive search. However, it more complex and longer than this alternative, which use VB6's split command.

If you use this alternative, the case sensitivity of the search will depend on whether you have option compare text set to true.

Instructions: Copy the declarations and code below and paste directly into your VB project.

Declarations:

Code: