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.