VB.Net program to demonstrate the InStr() function

Here, we are going to demonstrate the InStr() function in VB.Net.
Submitted by Nidhi, on December 04, 2020 [Last updated : March 05, 2023]

InStr() function in VB.Net

The InStr() function is used to search the substring in the specified string.

Syntax

InStr(Str, SubStr)

Parameter(s)

  • Str: Specified string that may contain given substring.
  • SubStr: Specified sub-string to be searched.

Return Value

The InStr() function will return an integer value, if a substring is found then it will return the starting index of the substring within the string.

VB.Net code to demonstrate the example of InStr() function

The source code to demonstrate the InStr() function is given below. The given program is compiled and executed successfully.

'VB.Net program to demonstrate the InStr() function.

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
Press any key to continue . . .

Explanation:

In the above program, we created a module Module1 that contains a Main() method. In the Main() method, we created three variables str, substr, ret that are initialized with "I love india", "love", and 0 respectively.

ret = InStr(str, substr)

In the above code, we used the InStr() function that will return an integer value, if a substring is found then it will return the starting index of the substring within the string.

VB.Net Basic Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.