How to check if the string ends with specific substring in Java?

A String is an object that represents an immutable sequence of characters and cannot be changed once created. The java.lang.String class can be used to create a string object.

We can use the endsWith() method of String class to check whether a string ends with a specific string or not, it returns a boolean value true or false.


public boolean endsWith(String prefix)


public class StringEndsWithSubStringTest {
   public static void main(String[] args) {
      String str = "WelcomeToTutorialsPoint";
      if(str.endsWith("Point")) {
         System.out.println("Ends with the specified word!");
      } else {
         System.out.println("Does not end with the specified word!");


Ends with the specified word! 

Updated on: 01-Dec-2023


Kickstart Your Career

Get certified by completing the course

Get Started