Java String endsWith() method with examples

The endsWith() method tells us whether a string ends with a specified string or not. This post will discuss the endsWith() method of the String class in detail.

  • Method declaration –public boolean endsWith(String suffix).
  • What does it do? It tells us whether the string ends with the specified string ( the string passed as the function argument ) or not.
  • What does it return? It will return true if the string ends with the specified string. Otherwise, it will return false.
Code example
public class Codekru {

	public static void main(String[] args) {

		String str1 = "codekru";

		String str2 = "kru";

		System.out.println("Does str1 ends with str2? " + str1.endsWith(str2));

	}
}

Output –

Does str1 starts with str2? true
Where can we use the endsWith() method?

We can use endsWith() method to know the extensions and thus the file type. Below is the program to illustrate the same

public class Codekru {

	public static void main(String[] args) {

		String str1 = "codekru.mp4";

		if (str1.endsWith("mp4")) {
			System.out.println("This is a video file");
		} else {
			System.out.println("Not in the mp4 format");
		}

	}
}

Output –

This is a video file
Internal implementation of the endsWith() method
    public boolean endsWith(String suffix) {
        return startsWith(suffix, length() - suffix.length());
    }

We can here endsWith() internally uses the startsWith() method to determine whether a string ends with a specific string or not.

Time Complexity of the endsWith() method

The time complexity of the endsWith() method will depend on the complexity of the startsWIth() method. And the complexity of the startsWith() method is O(n), so the average time complexity of the endsWith() method would also be O(n).

The What If scenarios

Q – What if try to use the endsWith() method to find an empty String?

It will return true, as illustrated by the below example.

public class Codekru {

	public static void main(String[] args) {

		String str1 = "codekru";

		String str2 = "";

		System.out.println("does str1 ends with str2? " + str1.endsWith(str2));

	}
}

Output –

does str1 ends with str2? true
Q – What if we try to use the endsWith() method on a null String?
public class Codekru {

	public static void main(String[] args) {

		String str1 = null;

		String str2 = "code";

		System.out.println("does str1 ends with str2? " + str1.endsWith(str2));

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.endsWith(String)" because "str1" is null
Q – What if we try to pass the null String into the function’s argument?

Here we will again get the NullPointerException.

public class Codekru {

	public static void main(String[] args) {

		String str1 = "codekru";

		String str2 = null;

		System.out.println("does str1 ends with str2? " + str1.endsWith(str2));

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "suffix" is null
	at java.base/java.lang.String.endsWith(String.java:1511)

If you want to learn more about Strings and its methods, we recommend you read this article.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Reference – https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#endsWith(java.lang.String)

Liked the article? Share this on

Leave a Comment

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