VB.NET StartsWith and EndWith String function

This blog defines the string functions StartWith and EndWith in VB.NET. We can use Contains method from string to search a substring. In the same way we can use StartWith, EndWith methods.

StartWith method

The VB.Net String StartsWith function returns a bool type value i.e. true or false based on the specified string value that is to be compared with the starting characters of given string value.

EndWith Method

The VB.Net String EndWith function returns a bool type value i.e. true or false based on the specified string value that is to be compared with the Ending characters of given string value.

For example

Module Module1

    Sub Main()

        Dim strings As String()

        Dim i As Integer

        strings = New String() {"started", "starting", "ended", "ending"}

        For i = 0 To strings.GetUpperBound(0)

            If strings(i).StartsWith("st") Then

                Console.WriteLine("search starting with st")

            End If

        Next

        For i = 0 To strings.GetUpperBound(0)

            If strings(i).EndsWith("ed") Then

                Console.WriteLine(" search end with ed")

            End If

        Next

    End Sub

End Module

OUTPUT

se1.gif