Visual Basic InStr
InStr is a powerful VB string function. It's the perfect way to search and test strings in robust Visual Basic 6.0 applications. This article shows how to call InStr to perform advanced text processing tasks quickly. The article also discusses case insensitivity, vbTextCompare, Option Compare Text and InStr related bugs.
In this article:
VB6 statements and constants in this article: InStr, InStrB, InStrRev, Option Compare, vbBinaryCompare, vbTextCompare.
InStr returns the first occurrence of a string inside another string.
InStr([start,] string1, string2 [, compare])
InStr returns one of the following values:
Crash warning. The
Basic uses of InStr
In the following examples we assume that
Locate text inside a string
position = InStr("avbprogram", "vb") ' Returns 2
position = InStr("avbprogramvb", "vb") ' Also returns 2
Locate the first character in a string
position = InStr("avbprogram", "v") ' Returns 2
Search in the rest of the string
The start parameter is useful to skip the start of the string. This allows us to skip known instances of the search string.
position = InStr(4, "avbprogramvb", "vb") ' Returns 11
It is also useful for optimizing calls to InStr. If we can start in the middle, the search will perform faster.
Test for existence of a substring
InStr is a quick and handy way to test whether a string contains another string. When the return value is positive, the string exists. When it is zero, it doesn't exist. The return value cannot be negative. The operator <> is the fastest way to decide between zero and positive.
exists = InStr("avbprogram", "vb") <> 0
Test for non-existence of a substring
If we want to make sure certain text does not exist inside another string, we simply reverse the existence test.
notexists = InStr("avbprogram", "vb") = 0
Text comparison: Beware!
Bug warning. The
Did you know that
How text comparison works
InStr with text comparison works with the following rules:
Due to rule 2, the following characters are unsafe: a, e, h, o, s, t. They are unsafe if any of the following characters can be expected in the input: ßÞþÆæŒœ. One can expect them in systems processing (foreign) names and addresses, for example.
As it happens, the following expressions are not equal, even though they could be. This means InStr will not locate them in place of the other:
Case insensitivity extends itself to a total of 673 Unicode characters. This includes all Latin characters and Latin characters with diacritic marks. It also includes Greek, Cyrillic, Armenian and Georgian characters and Unicode Roman numbers.
Text comparison examples
Text comparison also affects other functions: InStrB, InStrRev, Replace, Split and StrComp. The effects are not exactly the same across these functions, so be careful. To name an example, in function Replace "Þ" matches "th", but not "t" or "h" alone as with InStr.
Case insensitive search: The safe way
At times we need to search in a case insensitive way. As explained above, there is no safe ignore case flag for InStr. The safest way to call "case insensitive InStr" is to convert both input strings into lower case (or upper case).
position = InStr(LCase$("aVbprogram"), LCase$("VB")) ' Returns 2
Optimization: If one of the strings is already in lower or upper case, it is enough to convert the other.
position = InStr("avbprogram", LCase$("VB")) ' Returns 2
Further optimization: If you are going to call InStr several times with the same parameter, do your LCase$ first, store the result in a temporary variable and reuse the variable on each call. This way you save the repeated slow call to LCase$
Advanced uses for InStr
That was all about the the basic uses of InStr. Let's get to know what else we can do with it.
Test if character belongs to set
To see whether a character is one of given alternatives, call the following code:
result = InStr("ABC", "A") <> 0
This test tells us if "A" is one of "A", "B" or "C".
Test if string belongs to set
To see whether a longer piece of text is one of given alternatives, call the following kind of code:
result = InStr("-AB-CD-EF-", "-AB-") <> 0
This test tells us if "AB" is one of "AB", "CD" or "EF". Note the use of separators, "-" in this example.
Locate nth occurrence of a string
If you want to find the nth occurrence of a string, not simply the first one, set n to the desired number and run the following code:
position = 0 Do position = InStr(position + 1, "avbprogramvb", "vb") n = n - 1 Loop Until position = 0 Or n = 0
The loop keeps searching until the nth occurrence is found or there was no match. position tells the result.
To search for the nth occurrence after a certain start position, let position = start - 1.
Search with additional test
To conduct a search with additional conditions, run the following loop:
position = 0 Do position = InStr(position + 1, "avbprogram vb", "vb") If position = 0 Then Exit Do Loop Until test(...) = True
Each loop iteration performs a test on each finding. The loop exits when the test condition is met or when there are no (more) matches. The test will typically examine the found position to see whether it contains an acceptable value or not. You can use more complex tests such as the Like operator, tests that you cannot incorporate in the InStr call. In the above example, the test could determine whether "vb" is a word or a part of a longer word.
Optimizing calls to InStr
What comes to pure performance,
You can use the start parameter to optimize InStr. Use start to skip the beginning of the string when you know the match must be somewhere farther in the string.
For InStr performance details, please read Optimize string handling in VB6, Part II.
InStrRev – Reverse search
InStrRev searches the string in reverse order: from the end to the start. It locates the last occurrence of a string within another.
InStrRev(string1, string2 [, start] [, compare])
Note how the parameters are in a different order than with InStr. Also note that InStrRev does not reverse the input strings: "ABC" does not match "CBA".
InStrRev runs slowly, especially on a long string1. Use InStr when you can. More
InStrB – Byte search
InStrB treats the input and search strings as byte arrays, not as regular strings. As you may know, VB uses Unicode strings. InStrB lets you by-pass Unicode functionality and use byte arrays as if they were strings.
InStrB([start,] string1, string2 [, compare])
Instead of character position, InStrB returns the byte position of string2 in string1. InStrB should not be used for regular string processing. InStrB is relatively fast and it can be used for optimization, but it's tricky. Optimization with InStrB
Visual Basic InStr