String::EndsWith Method (String^, StringComparison)
Determines whether the end of this string instance matches the specified string when compared using the specified comparison option.
Assembly: mscorlib (in mscorlib.dll)
public: [ComVisibleAttribute(false)] bool EndsWith( String^ value, StringComparison comparisonType )
Parameters
- value
-
Type:
System::String^
The string to compare to the substring at the end of this instance.
- comparisonType
-
Type:
System::StringComparison
One of the enumeration values that determines how this string and value are compared.
Return Value
Type: System::Booleantrue if the value parameter matches the end of this string; otherwise, false.
| Exception | Condition |
|---|---|
| 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, 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. using namespace System; using namespace System::Threading; void Test(String^ testString, String^ searchString, StringComparison comparison) { String^ resultFormat = "\"{0}\" {1} with \"{2}\"."; String^ resultString = "does not end"; if (testString->EndsWith(searchString, comparison)) { resultString = "ends"; } Console::WriteLine(resultFormat, testString, resultString, searchString); } int main() { String^ introMessage = "Determine whether a string ends with another string, " + "using\ndifferent values of StringComparison."; array<StringComparison>^ comparisonValues = { StringComparison::CurrentCulture, StringComparison::CurrentCultureIgnoreCase, StringComparison::InvariantCulture, StringComparison::InvariantCultureIgnoreCase, StringComparison::Ordinal, StringComparison::OrdinalIgnoreCase}; Console::Clear(); Console::WriteLine(introMessage); // 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); // Perform two tests for each StringComparison for each (StringComparison stringCmp in comparisonValues) { Console::WriteLine("StringComparison.{0}:", stringCmp); Test("abcXYZ", "XYZ", stringCmp); Test("abcXYZ", "xyz", stringCmp); Console::WriteLine(); } } /* 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". */
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1