Java String endsWith() with examples

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

true
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

false
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

true


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.