Return The Number of Occurrences of a String/Char Within Another

Category:
String Manipulation
Type:
Snippets
Difficulty:
Beginning
Author:
Anonymous

Version Compatibility: Visual Basic 6, Visual Basic 5

More information:
Optimizing the code found at http://www.freevbcode.com/ShowCode.Asp?ID=1025, this author has written a function to count the number of instances a substring is found in a string. To test or demo this function, add two textbox controls and command button control to a form. With the click event of the command button, pass the text from the "source" textbox as the first argument and the text from the "substring" textbox as the second argument. There is an optional 3rd argument to dictate whether the function does a textual or binary comparison. Textual is the default.

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

Declarations:

Code: