VBA – IntStr String
The instr
function is designed to find the position of the first occurrence of a specified substring (string2
) within a given string (string1
).
string1
.
Return Value:
string2
within string1
.string2
is not found within string1
, it returns 0.string2
is an empty string (“”), it returns 1.Syntax:
Instr([start], string1, string2[, compare])
Parameter:
string1
to start the search. If omitted, the search begins at the first character.string2
.string1
.vbBinary
.vbTextCompare
.
Example
Dim str1 As String
Dim str2 As String
Dim pos As Integer
str1 = "Hello, World!"
str2 = "World"
pos = Instr(1, str1, str2)
' pos will be 7
pos = Instr(1, str1, "world") ' Case-sensitive, will return 0
pos = Instr(1, str1, "world", vbTextCompare) ' Case-insensitive, will return 7
Use Cases: