
EndsWith tests the last parts of strings. It finds strings in your C# program that have a certain ending sequence of characters. The .NET Framework provides this method on the string type. It is the simplest way to test for ending substrings.
This C# tutorial provides an example for the EndsWith string method.

First, the EndsWith method, like its counterpart the StartsWith method, has three overloaded method signatures. The first example here shows the simplest overload, the first one, which receives one parameter. To use EndsWith, you must pass the string you want to check the ending with as the argument. Here, an input string is tested for three ends.
Program that uses EndsWith [C#]
using System;
class Program
{
static void Main()
{
// The input string
string input = "http://site.com";
// Test these endings
string[] arr = new string[]
{
".net",
".com",
".org"
};
// Loop through and test each string
foreach (string s in arr)
{
if (input.EndsWith(s))
{
Console.WriteLine(s);
return;
}
}
}
}
Output
.comNotes. The input string above is the URL for a specific website. It ends with the letters ".com". Therefore, when the foreach loop tests all strings in the array, the second string ".com" will succeed. The EndsWith method returns true when "http://site.com" is tested for ".com".

The EndsWith method has three overloaded signatures. The first overload is demonstrated above. The second overload accepts two parameters, the second parameter being a StringComparison enumerated constant. You can use this to specify case-insensitive matching with EndsWith. Finally, the third overload allows more globalization options.
StringComparison Enum
We saw how you can use the EndsWith instance method on the string type that accepts one or more parameters. The C# example shows how you could use the method when processing data from the Internet. The method returns a Boolean value and by default performs a case-sensitive comparison. You can specify more options with the StringComparison enumerated type.
StartsWith Method String Type