Java String: endsWith() Method

public boolean endsWith(String suffix)

The endsWith() method is used to check if a specified string ends with the specified suffix.

Java Platform: Java SE 8


endsWith(String suffix)


Name Description Type
suffix the suffix. boolean

Return Value: 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.

Return Value Type: boolean

Pictorial presentation of Java String endsWith() Method

Java String: endsWith() Method

Example: Java String endsWith() Method

The following example shows the usage of java String() method.

public class Example {

public static void main(String[] args)
        String str1 = "Python Exercises";
        String str2 = "Python Exercise";

        // The String to check the above two Strings to see
        // if they end with this value (se).
        String end_str = "se";

        // Check first two Strings end with end_str
boolean ends1 = str1.endsWith(end_str);
boolean ends2 = str2.endsWith(end_str);
        // Display the results of the endsWith calls.
System.out.println("\"" + str1 + "\" ends with " +
            "\"" + end_str + "\"? " + ends1);
System.out.println("\"" + str2 + "\" ends with " +
            "\"" + end_str + "\"? " + ends2);


"Python Exercises" ends with "se"? false               
"Python Exercise" ends with "se"? true 

Java Code Editor:

Previous:copyValueOf Method
Next:equals Method

Follow us on Facebook and Twitter for latest update.