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

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus


Java tutorial for beginners Copyright © 2012