This resource has not currently been approved, and is not currently linked to from our directory of resources. It is being displayed here for preview by the author and moderators only.
Using InStr
Example 1 - Determining if a string contains a certain substring using the InStr Function
There are many uses for the InStr function to give a basic example we shall use it to ascertain whether or not a string contains any spaces. The function accepts four parameters:
The position to start searching from
The string to be searched
The string to search for
The comparison method (Optional), by default this will be a binary comparison
Syntax
InStr([Start Position, ]String to Check, String to Match[, Compare Method])
Return Value
The function returns a numeric value, if the search string is not found within the specified string this value will be Zero however, if the string is found, the function will be the starting
position of the first occurence of the substring.
Usage
The two common usages for the InStr function are:
a) To discover the starting position of one string within another
Dim intPos As Integer
intPos = InStr(1, strString, "example")
The variable intPos will now contain the starting pos of the string "example" which is 18
b) To ascertain whether a string is present within another before we do something
If InStr(1, strString, "example") Then
Else
End If
This is useful for performing a check on a string to make sure that it contains the parameter we need before we perform string manipulation upon it. An example of how this would be used may be to make sure that the given string contains one or more spaces before we perform an operation upon it which relies on there being spaces present:
If InStr(1, strString, " ") Then
End If