Home > Java > javaTutorial > body text

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

PHPz
Release: 2023-09-19 20:05:02
forward
1300 people have browsed it

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)
Copy after login

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

Output

Ends with the specified word!
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!