Java String endsWith example

String’s endWith method checks if String ends with specific suffix. It returns boolean datatype.

Method syntax:

public boolean endsWith(String suffix)

String endsWith Example:

package org.arpit.java2blog;

public class StringEndsWithExample {

 public static void main(String[] args) {
  String str = "java2blog";

  if (str.endsWith("blog")) {
   System.out.println("String ends with blog");
  } 
  else {
   System.out.println("String does not end with blog");
  }

  if (str.endsWith("BLOG")) {
   System.out.println("String ends with BLOG");
  } 
  else {
   System.out.println("String does not end with BLOG");
  }
 }

}

When you run above program, you will get below output:

String ends with blog
String does not end with BLOG

Please note that endsWith method is case sensitive as you can see in above example

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.