Java.lang Package Classes

Java String - endsWith() Method



The java.lang.String.endsWith() method is used to check whether the string ends with specified suffix or not. It returns true when the string ends with specified suffix, else returns false.

Syntax

public boolean endsWith(String suffix)

Parameters

suffix Specify the suffix.

Return Value

Returns true if the character sequence represented by the argument is a suffix of the character sequence represented by this object; false otherwise. Note that the result will be true if the argument is the empty string or is equal to this String object as determined by the equals(Object) method.

Exception

NA.

Example:

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

import java.lang.*;

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.lang - String