String.EndsWith Method (String, StringComparison)

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)

[ComVisibleAttribute(false)]
public bool EndsWith(
	string value,
	StringComparison comparisonType
)

Parameters

value
Type: System.String

A String object to compare to.

comparisonType
Type: System.StringComparison

One of the StringComparison 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.

ExceptionCondition
ArgumentNullException

value is null.

ArgumentException

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, 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.

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. 

using System;
using System.Threading;

class Sample 
{
    public static void Main() 
    {
    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 };

//
    Console.Clear();
    Console.WriteLine(intro);

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

// Determine whether three versions of the letter I are equal to each other.  
    foreach (StringComparison sc in scValues)
        {
        Console.WriteLine("StringComparison.{0}:", sc);
        Test("abcXYZ", "XYZ", sc);
        Test("abcXYZ", "xyz", sc);
        Console.WriteLine();
        }
    }

    protected static void Test(string x, string y, StringComparison comparison)
    {
    string resultFmt = "\"{0}\" {1} with \"{2}\".";
    string result = "does not end";
// 
    if (x.EndsWith(y, comparison))
        result = "ends";
    Console.WriteLine(resultFmt, x, result, y);
    }
}

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

*/

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft