Home  >  Article  >  Java  >  How to check if a string ends with a specific substring in Java?

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

PHPz
PHPzforward
2023-09-19 20:05:021345browse

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

A string is an object that represents an immutable sequence of characters and cannot be changed once created. String objects can be created using the java.lang.String class.

We can use the endsWith() method of the String class to check whether a string ends with a specific string , 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!

The above is the detailed content of How to check if a string ends with a specific substring in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete