Partager via


String.EndsWith Method (String, StringComparison)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
<ComVisibleAttribute(False)> _
Public Function EndsWith ( _
    value As String, _
    comparisonType As StringComparison _
) As Boolean
[SecuritySafeCriticalAttribute]
[ComVisibleAttribute(false)]
public bool EndsWith(
    string value,
    StringComparison comparisonType
)

Parameters

  • comparisonType
    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.

Exceptions

Exception Condition
ArgumentNullException

value is nulla null reference (Nothing in Visual Basic).

ArgumentException

comparisonType is not a StringComparison value.

Remarks

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, be the empty string (""), or match the end of this string. The type of comparison performed by the EndsWith method depends on the value of the comparisonType parameter.

Examples

The following code 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.Threading

Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      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}

      outputBlock.Text &= intro & vbCrLf

      ' Display the current culture because the culture-specific comparisons
      ' can produce different results with different cultures.
      outputBlock.Text &= String.Format("The current culture is {0}." & vbCrLf, _
                         Thread.CurrentThread.CurrentCulture.Name) & vbCrLf

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

   End Sub 'Main


   Protected Shared Sub Test(ByVal outputBlock As System.Windows.Controls.TextBlock, 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
      outputBlock.Text &= String.Format(resultFmt, x, result, y) & vbCrLf

   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.
'
'StringComparison.CurrentCulture:
'"abcXYZ" ends with "XYZ".
'"abcXYZ" does not end with "xyz".
'
'StringComparison.CurrentCultureIgnoreCase:
'"abcXYZ" ends with "XYZ".
'"abcXYZ" ends with "xyz".
'
'StringComparison.InvariantCulture:
'"abcXYZ" ends with "XYZ".
'"abcXYZ" does not end with "xyz".
'
'StringComparison.InvariantCultureIgnoreCase:
'"abcXYZ" ends with "XYZ".
'"abcXYZ" ends with "xyz".
'
'StringComparison.Ordinal:
'"abcXYZ" ends with "XYZ".
'"abcXYZ" does not end with "xyz".
'
'StringComparison.OrdinalIgnoreCase:
'"abcXYZ" ends with "XYZ".
'"abcXYZ" ends with "xyz".
'
// This example demonstrates the 
// System.String.EndsWith(String, StringComparison) method.

using System;
using System.Threading;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string intro = "Determine whether a string ends with another string, " +
                     "using\n  different values of StringComparison.";

      StringComparison[] scValues = {
        StringComparison.CurrentCulture,
        StringComparison.CurrentCultureIgnoreCase,
        StringComparison.InvariantCulture,
        StringComparison.InvariantCultureIgnoreCase,
        StringComparison.Ordinal,
        StringComparison.OrdinalIgnoreCase };

      outputBlock.Text += intro + "\n";

      // Display the current culture because the culture-specific comparisons
      // can produce different results with different cultures.
      outputBlock.Text += String.Format("The current culture is {0}.\n",
                         Thread.CurrentThread.CurrentCulture.Name) + "\n";

      // Determine whether three versions of the letter I are equal to each other. 
      foreach (StringComparison sc in scValues)
      {
         outputBlock.Text += String.Format("StringComparison.{0}:", sc) + "\n";
         Test(outputBlock, "abcXYZ", "XYZ", sc);
         Test(outputBlock, "abcXYZ", "xyz", sc);
         outputBlock.Text += "\n";
      }
   }

   protected static void Test(System.Windows.Controls.TextBlock outputBlock, string x, string y, StringComparison comparison)
   {
      string resultFmt = "\"{0}\" {1} with \"{2}\".";
      string result = "does not end";
      //
      if (x.EndsWith(y, comparison))
         result = "ends";
      outputBlock.Text += String.Format(resultFmt, x, result, y) + "\n";
   }
}

/*
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.

StringComparison.CurrentCulture:
"abcXYZ" ends with "XYZ".
"abcXYZ" does not end with "xyz".

StringComparison.CurrentCultureIgnoreCase:
"abcXYZ" ends with "XYZ".
"abcXYZ" ends with "xyz".

StringComparison.InvariantCulture:
"abcXYZ" ends with "XYZ".
"abcXYZ" does not end with "xyz".

StringComparison.InvariantCultureIgnoreCase:
"abcXYZ" ends with "XYZ".
"abcXYZ" ends with "xyz".

StringComparison.Ordinal:
"abcXYZ" ends with "XYZ".
"abcXYZ" does not end with "xyz".

StringComparison.OrdinalIgnoreCase:
"abcXYZ" ends with "XYZ".
"abcXYZ" ends with "xyz".

*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.