I am using the following function. This is not the most efficient memory, but it is very easy to understand, supports several comparison methods, only 4 lines, quickly, mainly works in VBA, finds not only individual characters, but any search string (I often look for VbCrLf (s)).
The only thing missing is the ability to start the search from another "Start"
Function inStC(myInput As String, Search As String, Optional myCompareMethod As Long = CompareMethod.Text) As Long If InStr(1, myInput, Search, myCompareMethod) = 0 Then Return 0 Return UBound(Split(myInput, Search,, myCompareMethod)) End Function
One thing that I like is a compact use case.
str="the little red hen" count=inStC(str,"e") 'count should equal 4 count=inStC(str,"t") 'count should equal 3
While I'm here, I would like to sew up my inStB function, which, instead of returning the number of rows, will simply return a boolean if a search string is present. I need this feature often, and it makes my code cleaner.
Function inStB(myInput As String, Search As String, Optional Start As Long = 1, Optional myCompareMethod As Long = CompareMethod.Text) As Boolean If InStr(Start, myInput, Search, myCompareMethod) > 0 Then Return True Return False End Function
Shodan Feb 12 '17 at 13:03 on 2017-02-12 13:03
source share