C# String EndsWith(String, StringComparison)

Description

String EndsWith(String, StringComparison) determines whether the end of this string instance matches the specified string when compared using the specified comparison option.

Syntax

String.EndsWith(String, StringComparison) has the following syntax.


[ComVisibleAttribute(false)]/*  w w  w. j av  a2s . co  m*/
public bool EndsWith(
  string value,
  StringComparison comparisonType
)

Parameters

String.EndsWith(String, StringComparison) has the following parameters.

  • value - The string to compare to the substring at the end of this instance.
  • comparisonType - One of the enumeration values that determines how this string and value are compared.

Returns

String.EndsWith(String, StringComparison) method returns true if the value parameter matches the end of this string; otherwise, false.

Example

The following example determines whether a string ends with a particular substring.


using System;/*w  w w  . j ava  2s.  com*/
using System.Threading;

class Sample 
{
    public static void Main() 
    {
    StringComparison[] scValues = {
        StringComparison.CurrentCulture,
        StringComparison.CurrentCultureIgnoreCase,
        StringComparison.InvariantCulture,
        StringComparison.InvariantCultureIgnoreCase,
        StringComparison.Ordinal,
        StringComparison.OrdinalIgnoreCase };

        foreach (StringComparison sc in scValues)
        {
            Console.WriteLine("StringComparison.{0}:", sc);
            Test("abcXYZ", "XYZ", sc);
            Test("abcXYZ", "xyz", sc);
        }
    }

    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);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version