< Prev
Next >



endsWith() method




As the name of the method goes, endsWith() method of String class determines if the value in a String object ends with a specified String or not.




Signature of endsWith()


public boolean endsWith(String st)






endsWith() example


In the upcoming code, we have a String object initialized to a value A Blue Moon and to check whether this String objects ends with a specific String value, we are passing a specific String value to endsWith() method for comparison with the invoked String object.
// endsWith() method of String.


class StringEndsWith
{
public static void main(String[] ar)
{
String str1= new String("A Blue Moon");
System.out.println("String is "  + str1);

boolean boo1 = str1.endsWith("Moon");
System.out.println("Does "+ str1 + " ends with Moon ? "+ boo1);  

boolean boo2 = str1.endsWith("on");
System.out.println("Does "+ str1 + " ends with on ? "+ boo2);

boolean boo3 = str1.endsWith("A Blue");
System.out.println("Does "+ str1 + " ends with A Blue ? "+ boo3);  
}
}


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 A Blue ? false


Program Analysis







Method endsWith() is case-sensitive.


Method endsWith() checks for comparison between String value passed to it and the invoked String object, comforming to the case sensitivity of each letter.
// endsWith() method of String.


class StringStartsWith
{
public static void main(String[] ar)
{
String str1= new String("A Mango Tree");
System.out.println("String is "  + str1);

boolean boo1 = str1.startsWith("Tree");
System.out.println("Does "+ str1 + " ends with Tree ? "+ boo1);  

boolean boo2 = str1.startsWith("tree");
System.out.println("Does "+ str1 + " ends with tree ? "+ boo2);

boolean boo3 = str1.startsWith("mango Tree");
System.out.println("Does "+ str1 + " ends with mango Tree ? "+ boo3);  
}
}


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




// endsWith() method of String.

class StringEndsWith2
{
public static void main(String[] ar)
{
String str1= new String("Kepler 452b");
System.out.println("String is "  + str1);

boolean boo1 = str1.endsWith("Kepler");
System.out.println("Does String "+ str1 + " ends with Kepler ? "+ boo1); 

boolean boo1 = str1.endsWith("452");
System.out.println("Does String "+ str1 + " ends with 452? "+ boo1); 

boolean boo2 = str1.endsWith("52b");
System.out.println("Does String "+ str1 + " ends with 52b ? "+ boo2);

boolean boo3 = str1.endsWith("Kepler 452b");
System.out.println("Does String "+ str1 + " ends with Kepler 452b ? "+ boo3);  
}
}


Output is :


String is Kepler 452b
Does String Kepler 452b ends with Kepler ? false
Does String Kepler 452b ends with 452 ? false
Does String Kepler 452b ends with 452b ? true
Does String Kepler 452b ends with Kepler 452b ? true


Program Analysis





Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< startsWith() method
length() method >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2