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.

Syntax

public boolean endsWith(String prefix)

Example

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!");
      }
   }
}

Output

Ends with the specified word! 

raja
raja

e

Updated on: 01-Dec-2023

895 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements