Friday, October 10, 2025
HomeLanguagesJavaJava String endsWith() with examples

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.

Syntax of endsWith()

The syntax of String endsWith() method in Java is:

public boolean endsWith (String suff);

Parameters

  • suff: specified suffix part

Returns

  • If the string ends with the given suff, it returns true.
  • If the string does not end with the stuff, it returns false.

Examples of String endsWith() in Java

Example 1: Java Program to show the working of endsWith() method

Java




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


Output

true

Example 2:

Java




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


Output

false

Example 3:

Java




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


Output

true

Example 4: Java Program to check if the string ends with empty space at the end.

Java




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


Output

false

Example 5: Java Program to show the NullPointerException when we pass null as the parameters to endsWith()

Java




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


Output

Exception in thread "main" java.lang.NullPointerException
    at java.base/java.lang.String.endsWith(String.java:1485)
    at Gfg3.main(Gfg3.java:13)
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7101 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS