Time and again, some or the other team member in my team encounters a
situation where one need to find the number of instances of a substring in a
given string. I have found the simplest, quickest and the smartes way to do so
in just a line of code.
Here it is... (Method 1)
SubStringCount = Ubound(Split(StringName, delimeter))
Exampli gratia:
Finds the occurences of substring "m" in the string "Hiduism
promotes harmony"
msgbox SubStringCount
returns 3 in this case.
Another reliable mehod would be to use a RegExp Object.
Method 2
str="Hiduism promotes harmony"
Set Regex=new RegExp
Regex.pattern="m"
Regex.global=true
Set Matches=Regex.execute(str)
msgbox Matches.count
returns 3 in this case again.
Another method (Method 3) would be use a Instr(Start, StringName,
SubStringToSearch) function. Start can be replaced every time with the
instances of SubStringToSearch. The couner shall be increased in parallel with
every instance of SubString found.
This method (Method 4) is suggested by my team member Spoorthi K.
SubStringCount = Len(StringName) - Replace(StringName, SubStringToSearch,
"")
Well, the above code need to be adjusted depending on the length of the
SubString.
Pretty much in this post. See you again in another post.