Using InStr Function in VB
InStr in VB.NET / VB6 is a very powerful method to search for the occurance of certain character or word in a string. You can use the InStr method in VB.NET / VB6 code as shown below. The result of using InStr will be in Integer form displaying the position that the string you find occurs at. myString is the string that you want to search against. FindString is the character or word you are finding in myString. OccurancePost is the position in which the finding is located.
OccurancePost = InStr(myString, FindString)
The simple example illustrated below shows how InStr method works in VB.
Dim OccurancePost As Integer '---Declare variables
Dim myString, FindString As String
myString = "Hello David! Welcome to the world" '--- the sentence
FindString = "David" '----- data to look for in the sentence
OccurancePost = Instr(myString, FindString) '--- InStr method
MsgBox(OccurancePost) '--- the result will be 7.
From the code above, we can see that the word David occurs at the 7th letter from the left (position 7) in the sentence. However, if the word that you intend to find does not appear in the sentence, InStr method will return a value of zero (0).
You can include the following code to cater for the case as below.
If OccurancePost = 0 Then
MsgBox("The word that you intend to look for cannot be found")
End If
That briefly explains how InStr works in VB.NET or VB6
Subscribe to:
Post Comments (Atom)

0 comments
Post a Comment