IndexOf() and LastIndexOf() methods in VB.NET

This blog defines IndexOf() and LastIndexOf() methods.

Indexof() method

To find the index of the first occurrence of the specified substring. Use the ideal IndexOf String Function for this, which is available in the .NET Framework's base class library.

IndexOf(String str) As Integer

Here,
str - The parameter string to check its occurrences

LastIndexOf() method

To find index position of the last occurrence of a specified String within this instance.

For example

Module Module1

    Sub Main()

        Dim myStrings As String() = {"hello", "be", "or", "hello", "to", "be"}

        Dim myString As String = [String].Join(".", myStrings)

        Dim index As Integer = myString.IndexOf("he")

        Console.WriteLine("""be"" first occurs at index " & index & " of myString")

        index = myString.LastIndexOf("lo")

        Console.WriteLine("""be"" last occurs at index " & index & " of myString")

        index = myString.IndexOf("b"c)

        Console.WriteLine("'b' first occurs at index " & index & " of myString")

        index = myString.LastIndexOf("b"c)

        Console.WriteLine("'b' last occurs at index " & index & " of myString")

 

    End Sub

End Module