Java Program to check the end of a string


To check the end of a string, use the endsWith() method.

Let’s say the following is our string.

String str = "demo";

Now, check for the substring “mo” using the endsWith() method in an if-else condition.

if(str.endsWith("mo")) {
   System.out.println("The string ends with the word mo");
} else {
   System.out.println("The string does not end with the word mo");
}

The following is the complete example with output.

Example

 Live Demo

public class Demo {
    public static void main(String[] args) {
       String str = "demo";
       if(str.endsWith("mo")) {
          System.out.println("The string ends with the word mo");
       } else {
          System.out.println("The string does not end with the word mo");
       }
    }
}

Output

The string ends with the word mo

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 26-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements