Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

C# EndsWith Method

This C# tutorial provides an example for the EndsWith string method.

EndsWith tests the last parts of strings.

It finds strings in your program that have a certain ending sequence of characters. The .NET Framework provides this method on the string type. It is a simple way to test for ending substrings.

Example. First, the EndsWith method, like its counterpart the StartsWith method, has three overloaded method signatures. The first example here shows the simplest and first overload, which receives one parameter.


Tip: To use EndsWith, you must pass the string you want to check the ending with as the argument.

Next: An input string is tested for three ends. We detect the ending extension of the URL.

C# program that uses EndsWith

using System;

class Program
    static void Main()
	// The input string
	string input = "";

	// Test these endings
	string[] arr = new string[]

	// Loop through and test each string
	foreach (string s in arr)
	    if (input.EndsWith(s))



The input string above is the URL for a specific website. It ends with the letters ".com". When we test all strings in the array, the second string ".com" will succeed. EndsWith returns true when "" is tested for ".com".

String ArrayBoolTrue

Overloads. 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.


Tip: You can use StringComparison to specify case-insensitive matching with EndsWith—try OrdinalIgnoreCase.


Finally: The third overload allows more globalization options, which are essential if non-English data will be encountered.

Summary. We used the EndsWith method. 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.