Home >>VB.Net Built-in Functions >VB.Net program to demonstrate the InStr() function
The function InStr() is used to search for a substring in the string specified.
Syntax :
InStr(Str, SubStr)
Parameters :
Return Value :
Program :
The source code for demonstrating the function InStr() is given below. The program given is compiled and successfully executed.
Module Module1
Sub Main()
Dim str As String = "I love india"
Dim substr As String = "love"
Dim ret As Integer = 0
ret = InStr(str, substr)
If ret = 0 Then
Console.WriteLine("Substring {0} is not found", substr)
Else
Console.WriteLine("Substring {0} is found at index {1}", substr, ret)
End If
End Sub
End Module
Explanation:
We created a Module1 module in the program above that contains the Main() method. We created three variables str, substr, ret in the Main() method that are initialized with "I love india", "love" and 0 respectively.
ret = InStr(str, substr)
We used the InStr() function in the code above, which returns an integer value, and if a substring is found, it returns the starting index of the substring within the string.