Java Tutorial Java Advanced Java References

Java String - endsWith() Method



The Java endsWith() method is used to check whether the string ends with specified character(s) or not. It returns true when the string ends with specified character(s), else returns false.

Syntax

public boolean endsWith(String chars)

Parameters

chars Character(s) to check whether the string ends with or not.

Return Value

Returns true if the string ends with specified character(s), else returns false.

Exception

NA.

Example:

In the example below, endsWith() method is used to check whether the string called MyString ends with specified character(s) or not.

public class MyClass {
  public static void main(String[] args) {
    String MyString = "Hello World!.";

    System.out.println(MyString.endsWith("Hello")); 
    System.out.println(MyString.endsWith("World!."));  
  }
}

The output of the above code will be:

false
true

❮ Java String Methods