Author Topic: Search for string in VB6 by InStr  (Read 7480 times)

Offline admin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 296
    • View Profile
Search for string in VB6 by InStr
« on: June 23, 2010, 07:59:49 PM »
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.

InStr syntax
InStr returns the first occurrence of a string inside another string.

Code: [Select]
InStr([start,] string1, string2 [, compare])start
Optional. Character position where the search starts. If omitted, the search starts at the start of string1, position 1. Valid input values: 1 to Len(string1)
string1
The string to search in. Can be Null.
string2
The string to find. This is the text you want to find inside string1. It is usually shorter than string1. Can be Null.
compare
Optional. Specifies the type of string comparison.
•vbBinaryCompare (0) performs a fast binary comparison where each character matches only itself. This is the default.
•vbTextCompare (1) performs a text comparison.
•vbDatabaseCompare (2). For Microsoft Access (Windows only), performs a comparison based on information contained in your database.
•If you omit compare, InStr will use the setting of the Option Compare statement.
Binary comparison is the safest option. Text comparison rules vary by the system locale. In text comparison, what matches on one computer may not match on another.

Return value
InStr returns one of the following values:
•Null if either string1 or string2 is Null.
•start if string2 is empty.
•Character position of the first occurrence of string2 in string1.
•Zero if string2 was not found in string1.

Special cases:
•InStr(string1, "") returns 1 if string1 is not empty.
•InStr("", "") returns 0.
•If start exceeds the length of string1, there is never a match and InStr returns 0