Skip to content
Related Articles

Related Articles

Java String endsWith() with examples

Improve Article
Save Article
Like Article
  • Last Updated : 04 Dec, 2018

The java string endsWith() method checks whether the string ends with a specified suffix.This method returns a boolean value true or false.

Signature:

public boolean endsWith(String suff)     

Parameter:

suff- specified suffix part 

Return:

true or false

Example:To show working of endsWith() method




// Java program to demonstrate
// working of endsWith() method
  
class Gfg1 {
    public static void main(String args[])
    {
        String s = "Welcome! to GeeksforGeeks";
  
        // Output will be true as s ends with Geeks
        boolean gfg1 = s.endsWith("Geeks");
        System.out.println(gfg1);
    }
}

Output:

true




// Java program to demonstrate
// working of endsWith() method
  
class Gfg2 {
    public static void main(String args[])
    {
        String s = "Welcome! to GeeksforGeeks";
  
        // Output will be false as s does not
        // end with "for"
        boolean gfg2 = s.endsWith("for");
        System.out.println(gfg2);
    }
}

Output:

false




// Java program to demonstrate
// working of endsWith() method
  
class Gfg3 {
    public static void main(String args[])
    {
        String s = "Welcome! to GeeksforGeeks";
  
        // Output will be true if the argument
        // is the empty string
        boolean gfg3 = s.endsWith("");
        System.out.println(gfg3);
    }
}

Output:

true

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!