Visual Basic String IndexOf Method

In visual basic, the string IndexOf method is useful to return the index of the first occurrence of a specified character in the given string.

 

Generally, in visual basic the string IndexOf method will start searching for the specified character starting from the Zero (0) position and return the first occurrence of the specified character in the given string.

 

In case, if you want to change the specified character search starting position and to examine only the specified number of character positions in the string, we can do it by specifying the required start position and number of character positions to search in the string IndexOf method.

 

Following is the pictorial representation of searching for a specified character position in the given string using the IndexOf() method in a visual basic programming language.

 

Visual Basic String IndexOf() Method Representation Diagram

 

If you observe the above diagram, we are trying to get the character “s” position from the given string “Suresh Dasari” and it returned the position as 4.

 

In visual basic, the string IndexOf() method will perform a case-sensitive search to get the specified character position that’s the reason we got the result as 4 even the character “S” exists at the starting position 0.

Visual Basic String IndexOf Method Syntax

Following is the syntax of defining a string IndexOf method to get the specified character position in a visual basic programming language.

 

Public Function IndexOf(ByVal ch As Char) As Integer

Public Function IndexOf(ByVal ch As Char, ByVal startIndex As Integer) As Integer

Public Function IndexOf(ByVal ch As Char, ByVal startIndex As Integer, ByVal count As Integer) As Integer

Public Function IndexOf(ByVal ch As Char, ByVal comparisonType As StringComparison) As Integer

Public Function IndexOf(ByVal str As String) As Integer

Public Function IndexOf(ByVal str As String, ByVal startIndex As Integer) As Integer

Public Function IndexOf(ByVal str As String, ByVal startIndex As Integer, ByVal count As Integer) As Integer

 

Public Function IndexOf(ByVal str As String, ByVal comparisonType As StringComparison) As Integer

If you observe the above syntaxes, the IndexOf method will return the index position of the specified character or a string in the given string and we can change the starting position of character's search and length of characters to search based on our requirements.

 

The string IndexOf method will return an index position of specified character if that character is found. In case, the specified character is not found, then it will return -1.

Visual Basic String IndexOf Method Example

Following is the example of using the string IndexOf() method to return an index position of the specified character from the given string visual basic programming language.

 

Module Module1

    Sub Main()

        Dim name As String = "Suresh Dasari"

        Console.WriteLine("Character s Index Position: {0}", name.IndexOf("s"))

        Console.WriteLine("Ignore Case: {0}", name.IndexOf("s", StringComparison.OrdinalIgnoreCase))

        Console.WriteLine("Change Search Start Position: {0}", name.IndexOf("s", 5))

        Console.WriteLine("Characters Length Reult: {0}", name.IndexOf("s", 5, 3))

        Console.WriteLine("String Position: {0}", name.IndexOf("Dasa"))

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we used a IndexOf() method to find the index position of a defined character in string “Suresh Dasari” with multiple conditions.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic String IndexOf() Method Example Result

 

This is how we can use the string IndexOf() method to get the position of specified character from the given string in a visual basic programming language.