TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

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.

StartsWith

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 = "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

.com

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 "http://site.com" 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.

Enum

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

StringComparison

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf