Home >>VB.Net Built-in Functions >VB.Net program to demonstrate the InStr() function

VB.Net program to demonstrate the InStr() function

Write a 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 :

  • Str : The specified string that can contain the substring given.
  • SubStr : The sub-string specified to search for.

Return Value :

  • The InStr() function returns an integer value, which returns the starting index of a substring within the string if a substring is identified.

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
	
Output:
Substring love is found at index 3

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.


No Sidebar ads