AfterI came here after reading a related post in the retailcoder blog -, Enhancing VBA String Handling. - and making some tests, I created this personal version ofTake into account that in the InStr function if String2 is an empty string then the returned value is the Start position (different from 0), so the function may erroneously return True in that case. Below the first functionway I implement it with that consideration. In many of my situations is important to know if a text is contained within another from specific positions:
'@Description "Returns True if found an ocurrence of one string within another from a specific position. False otherwise."
Public Function Contains(ByVal TextToLookInStringToLookIn As String, _
ByVal TextToLookForStringToLookFor As String, _
Optional ByVal FromPosition As Long = 1, _
Optional ByVal CaseSensitive As Boolean = False) _
As Boolean
If TextToLookForStringToLookFor <> vbNullString Then
Dim ComparisonMethod As VbCompareMethod
If CaseSensitive Then
ComparisonMethod = vbBinaryCompare
Else
ComparisonMethod = vbTextCompare
End If
Contains = CBool(InStr(FromPosition1, TextToLookInStringToLookIn, TextToLookForStringToLookFor, ComparisonMethod))
End If
End Function
In the InStr function if String2 (TextToLookFor in the function) is an empty string then the returned value is the start position (FromPosition in the function) so the function may erroneously return True in that cases.