Duration (Pacific Standard Time):
To (Pacific Standard Time):
  • None
User Action:
  • None

String.EndsWith Method (String, StringComparison)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Determines whether the end of this string instance matches the specified string when compared using the specified comparison option.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

Public Function EndsWith (
	value As String,
	comparisonType As StringComparison
) As Boolean


Type: System.String

The string to compare to the substring at the end of this instance.

Type: System.StringComparison

One of the enumeration values that determines how this string and value are compared.

Return Value

Type: System.Boolean

true if the value parameter matches the end of this string; otherwise, false.

Exception Condition

value is null.


comparisonType is not a StringComparison value.

The EndsWith method compares the value parameter to the substring at the end of this string and returns a value that indicates whether they are equal. To be equal, value must be a reference to this same string, must be the empty string (""), or must match the end of this string. The type of comparison performed by the EndsWith method depends on the value of the comparisonTypeparameter.

The following example determines whether a string ends with a particular substring. The results are affected by the choice of culture, whether case is ignored, and whether an ordinal comparison is performed.

' This example demonstrates the 
' System.String.EndsWith(String, StringComparison) method.

Imports System
Imports System.Threading

Class Sample
    Public Shared Sub Main() 
        Dim intro As String = "Determine whether a string ends with another string, " & _
                              "using" & vbCrLf & "  different values of StringComparison."

        Dim scValues As StringComparison() =  { _
                        StringComparison.CurrentCulture, _
                        StringComparison.CurrentCultureIgnoreCase, _
                        StringComparison.InvariantCulture, _
                        StringComparison.InvariantCultureIgnoreCase, _
                        StringComparison.Ordinal, _
                        StringComparison.OrdinalIgnoreCase }

        ' Display the current culture because the culture-specific comparisons
        ' can produce different results with different cultures.
        Console.WriteLine("The current culture is {0}." & vbCrLf, _

        ' Determine whether three versions of the letter I are equal to each other. 
        Dim sc As StringComparison
        For Each sc In  scValues
            Console.WriteLine("StringComparison.{0}:", sc)
            Test("abcXYZ", "XYZ", sc)
            Test("abcXYZ", "xyz", sc)
        Next sc

    End Sub 'Main

    Protected Shared Sub Test(ByVal x As String, ByVal y As String, _
                              ByVal comparison As StringComparison) 
        Dim resultFmt As String = """{0}"" {1} with ""{2}""."
        Dim result As String = "does not end"
        If x.EndsWith(y, comparison) Then
            result = "ends"
        End If
        Console.WriteLine(resultFmt, x, result, y)

    End Sub 'Test
End Class 'Sample

'This code example produces the following results:
'Determine whether a string ends with another string, using
'  different values of StringComparison.
'The current culture is en-US.
'"abcXYZ" ends with "XYZ".
'"abcXYZ" does not end with "xyz".
'"abcXYZ" ends with "XYZ".
'"abcXYZ" ends with "xyz".
'"abcXYZ" ends with "XYZ".
'"abcXYZ" does not end with "xyz".
'"abcXYZ" ends with "XYZ".
'"abcXYZ" ends with "xyz".
'"abcXYZ" ends with "XYZ".
'"abcXYZ" does not end with "xyz".
'"abcXYZ" ends with "XYZ".
'"abcXYZ" ends with "xyz".
Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top