String endsWith()

The String endsWith() method is used to check the current string is ends with a given string. It means the method returns true if the current string ends with a suffix otherwise it returns false. The return type of endsWith() is boolean. It returns only true or false value. Let’s see how does String endsWith() method work?

String endsWith
stringName.endsWith(anotherStringName)

stringName: The name of string which will be check whether it ends with the given String or not.
anotherStringName: It is a string that will be provided by a user to check whether the current string ends with the same string or not.

class ExampleOfEndsWith
{  
    public static void main(String args[])
    {    
	String string1 = "JAVA WINGS";  
        // It will returns true
	System.out.println("Is Strings ends with WINGS = "+ string1.endsWith("WINGS"));
	// It will returns false
	System.out.println("Is Strings ends with JAVA = "+ string1.endsWith("JAVA")); 
	// It will returns true
	System.out.println("Is Strings ends with NGS = "+ string1.endsWith("NGS")); 
     }
}  

Output: Is Strings ends with WINGS = true
Is Strings ends with JAVA = false
Is Strings ends with NGS = true

Leave a Comment