Check If String Ends With Specific Substring in Java



This article will discuss how to check if a string ends with a specific substring, which means we need to verify whether the last part of the string matches a given sequence of characters or not.

For example, if the given input string values are "Gaint","allegiant", "applicant", "combatant", "elephant", and if the substring is "ant", the result would be true for all the given values.

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.

There are several ways to check if a string ends with a specific substring, such as -

Check Specific Ending in String Using endsWith()

In Java, one of the common ways to check if a string ends with a specific substring(suffix) is by using the endsWith() method. This method accepts a string value representing the suffix, and if the current string ends with the given suffix, this method returns "true"; else it returns false.

Syntax

The following is the syntax of the endsWith() method -

public boolean endsWith(String suffix)

Here, the suffix is the substring to be checked at the end of the string.

Example

The following example will use the endsWith() method to check whether the string "Welcome To TutorialsPoint" ends with the substring "Point" or not:

public class StringEndsWithSubStringTest {
   public static void main(String[] args) {
      String str = "WelcomeToTutorialsPoint";
      System.out.println("The given string is: " + str);
      String sub_str = "Point";
      System.out.println("The substring needs to check: " + sub_str);
      
      //using endsWith() method
      if(str.endsWith(sub_str)) {
         System.out.println("The string '" + str + "' ends with substring '" + sub_str +"'");
      } else {
         System.out.println("The string '" + str + "' is not ends with substring '" + sub_str +"'");
      }
   }
}

The above program produces the following output -

The given string is: WelcomeToTutorialsPoint
The substring need to check: Point
The string 'WelcomeToTutorialsPoint' ends with substring 'Point'

Using lastIndexOf() Method

This is another easy way to check if a string ends with a specific substring. The lastIndexOf() method is used to find the last occurrence of a specified substring within an original string.

Syntax

Following is the syntax of the lastIndexOf() method:

public int lastIndexOf(String str)

Here, the str is a substring that needs to be searched.

Note: This method has different syntaxes, but we will use this one as it is suitable for the article.

Example

The program below checks if the string "Hey! How are you" ends with the substring "you" using the lastIndexOf() method. It compares the last index of "you" with the position it would be at if it were at the end of the string. If both match, it returns true -

public class StringEndsWithSubStringTest {
   public static void main(String[] args) {
      String str = "Hey! How are you";
      String sub_str = "you";
      
      System.out.println("The given string is: " + str);
      System.out.println("The substring needs to check: "+ sub_str);
      // Checking if the string end with the specified substring
      boolean result = str.lastIndexOf(sub_str) == (str.length() - sub_str.length());
      System.out.println("Does the string '" + str + "' ends with substring '" + sub_str + "'? " + result);
   }
}

Following is the output of the above program -

The given string is: Hey! How are you
The substring needs to check: you
Does the string 'Hey! How are you' ends with substring 'you'? true

Using Regex Expression and the matches() Method

We can also check if the string ends with a specific substring using a regular expression. In Java, a Regular Expression (or Regex) is a pattern (or filter) that describes a set of strings that match the pattern. The matches() method of the Pattern class in Java checks if the entire input string matches the given regular expression.

Example

In the following program, we use a regular expression and the Pattern.matches() function to check if the string "Hello World" ends with the specified substring "World". Here, the expression ".*" allows any characters before "World", and the $ symbol ensures that "World" appears at the end of the string -

import java.util.regex.*;

public class StringEndsWithSubStringTest {
   public static void main(String[] args) {
      String str = "Hello World";
      String regex = "World$";
      System.out.println("The original string is: " + str);
      System.out.println("The substring to check: " + regex);
      
      boolean result = Pattern.matches(".*" + regex, str);
      
      System.out.println("Does the string end with the substring 'World'? " + result);
   }
}

The above program displays the following output -

The original string is: Hello World
The substring to check: World$
Does the string end with the substring 'World'? true
Updated on: 2025-05-05T14:19:02+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements