Advertisement



< Prev
Next >



C# EndsWith() method




The opposite of the StartsWith() method is the EndsWith() method of String class, which determines if the value in a String object ends with a specified String or not.




Some overloads of EndsWith() method



Methods Description
EndsWith(String)
This method determines whether the ending of the invoked String matches with String object passed to this method.

EndssWith(String, StringComparison)
This method determines whether the ending of the invoked String matches with String object passed to this method, when compared using specified StringComparison.

EndsWith(String, Boolean, CultureInfo)
This method determines whether the ending of the invoked String matches with String object passed to this method, when compared using specified CultureInfo.






Example of EndsWith(String)


Let us explain the first overload version of EndsWith() method. In the upcoming code, we have a String object initialized to a value A Blue Moon and we are going to check whether it ends with a specific String value by calling the EndsWith() method.

//C# EndsWith() method of String.


using System;

class StringEndsWith
{
public static void Main()
{
	String str1 = "A Blue Moon";
	Console.WriteLine("String is: "  + str1);

	bool boo1 = str1.EndsWith("Moon");
	Console.WriteLine("Does "+ str1 + " ends with Moon ? "+ boo1);  

	boo1 = str1.EndsWith("on");
	Console.WriteLine("Does "+ str1 + " ends with on ? "+ boo1); 

	boo1 = str1.EndsWith("Moons");
	Console.WriteLine("Does "+ str1 + " ends with Moons ? "+ boo1); 

	boo1 = str1.EndsWith("A Blue");
	Console.WriteLine("Does "+ str1 + " ends with A Blue ? "+ boo1);  
}
}


Output is :


String is: A Blue Moon
Does A Blue Moon ends with Moon ? True
Does A Blue Moon ends with on ? True
Does A Blue Moon ends with Moons ? False
Does A Blue Moon ends with A Blue ? False


Program Analysis







The EndsWith(String) performs a case-sensitive comparison.


By default, the first overload version of EndsWith() checks for comparison between String value passed to it and the invoked String object by conforming to the case sensitivity of each letter.

//C# EndsWith() method of String.

using System;

class StringEndsWithEx
{
public static void Main()
{
	String str1 = "A Mango Tree";
	Console.WriteLine("String is: "  + str1);

	bool boo1 = str1.EndsWith("Tree");
	Console.WriteLine("Does "+ str1 + " ends with Tree ? "+ boo1);  

	boo1 = str1.EndsWith("tree");
	Console.WriteLine("Does "+ str1 + " ends with tree ? "+ boo1);

	boo1 = str1.EndsWith("mango Tree");
	Console.WriteLine("Does "+ str1 + " ends with mango Tree ? "+ boo1);  
}
}


Output is :

String is: A Mango Tree
Does A Mango Tree ends with Tree ? True
Does A Mango Tree ends with tree ? False
Does A Mango Tree ends with mango Tree ? False


Program Analysis





Advertisement




Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement