C# .EndsWith()
Published Mar 28, 2023
Contribute to Docs
The .EndsWith() method determines whether the end of the string instance matches the specified character or string. Additional parameters may be supplied to define the criteria for a match.
Syntax
// Determines whether the String instance's last character matches the specified character
String.EndsWith(char);
// Determines whether the end of String instance matches the specified string
String.EndsWith(string);
// Same as above but uses additional case-sensitive and culture-sensitive
String.EndsWith(string, ignoreCase, culture)
// Same as above but uses an enumeration to determine the case- and culture-sensitive criteria
String.EndsWith(string, comparisonType)
.EndsWith() is a static method of the String object. It takes the following arguments:
charis an instance of theCharstructure; represents a single letter.stringis an instance of theStringobject.ignoreCaseis a boolean; iftruethen case is ignored for comparison (e.g., ‘a’ == ‘A’).cultureis an instance of theSystem.Globalization.CultureInfoclass which includes culture-specific sort order rules.comparisonTypeis an element of theStringComparisonenumeration which encapsulates the case- and culture-specific criteria; available fields include:CurrentCulturesets the current culture rules.CurrentCultureIgnoreCasesets the current culture rules but ignores case.InvariantCulturesets the invariant culture’s sorting rules (it’s a culture that doesn’t change based on the user’s location).InvariantCultureIgnoreCasesets the invariant culture rules but ignores case.Ordinalsets ordinal (binary) sort rules to compare strings.OrdinalIgnoreCasesets ordinal (binary) sort rules to compare strings but ignores case.
Example
The following example illustrates a few of the ways the String.EndsWith() method may be used to evaluate the end of a string.
using System;using System.Threading;public class Example{public static void Main(){string baseString = "AbCdEfG";string compareEnd = "efg";bool result;// String.EndsWith(string)result = baseString.EndsWith(compareEnd);Console.WriteLine("Simple compare: {0}", result.ToString());// String.EndsWith(string, ignoreCase, culture)result = baseString.EndsWith(compareEnd, true, Thread.CurrentThread.CurrentCulture);Console.WriteLine("Compare using Case/Culture: {0}", result.ToString());// String.EndsWith(string, comparisonType)result = baseString.EndsWith(compareEnd, StringComparison.InvariantCulture);Console.WriteLine("Compare using Enumeration: {0}", result.ToString());}}
This is what is printed to the console:
Simple compare: FalseCompare using Case/Culture: TrueCompare using Enumeration: False
Codebyte Example
In this example, the months array is populated with the months of the year, and the .EndsWith() method is used to determine the number of months that end in the letter ‘y’:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C# on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
- Beginner Friendly.15 hours