Java String endsWith()

The syntax of the string endsWith() method is:

string.endsWith(String str)

Here, string is an object of the String class.


endsWith() Parameters

The endsWith() method takes a single parameter.

  • str - check whether string ends with str or not

endsWith() Return Value

  • returns true if the string ends with the given string
  • returns false if the string doesn't end with the given string

Example: Java endsWith() Without Offset Parameter

class Main {
  public static void main(String[] args) {

    String str = "Java Programming";

    System.out.println(str.endsWith("mming")); // true
    System.out.println(str.endsWith("g")); // true
    System.out.println(str.endsWith("a Programming")); // true

    System.out.println(str.endsWith("programming")); // false
    System.out.println(str.endsWith("Java")); // false
  }
}

As you can see from the above example, endsWith() takes case (lower case and upper case) into consideration.


If you need to check whether the string begins with the specified string or not, use the Java String startsWith() method.

Did you find this article helpful?