Table of contents
1.
Introduction
2.
String endsWith()
3.
Examples 
3.1.
Example 1
3.2.
Example 2
3.3.
Example 3
3.4.
Example 4
3.5.
Example 5
4.
Frequently Asked Questions
4.1.
What does endsWith() do in Java?
4.2.
How do you know if a string ends with certain characters?
4.3.
How can you check whether a string starts with a certain character?
5.
Conclusion
Last Updated: Mar 27, 2024

String endsWith()

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, strings are objects representing a sequence of character values. An array of characters work the same as a string in Java. The Java String class has a lot of methods to perform operations on strings like compare(), concat(), equals(), split(), length(), replace(), compareTo(), endsWith(). In this article, we will learn about the string endsWith() method with the help of code examples. Let us dive into the topic.

String endsWith()

The method Java String endsWith() checks whether the string ends with a user-specified substring. Upon matching the specific suffix, it returns the result in boolean value true if it is matched or false otherwise.

Syntax:

public boolean endsWith(String suff) 

Parameter: suffix: Sequence of character

Returns: true or false

Examples 

Let us see a few examples to demonstrate the working of the string endsWith() method.

Example 1

Code:

class Ninja1 {
    public static void main(String args[])
    {
        String str = "HelloWorld";
        boolean var1 = str.endsWith("World");
        System.out.println(var1);
    }
}

Output:

True

Explanation:

In the first example, we check whether the given string str ends with “World”. Since str contains the required sequence of characters, hence the result is true, stored in boolean variable var1.

Example 2

Code:

class Ninja2 {
    public static void main(String args[])
    {
        String str = "HelloWorld";
        boolean var2 = str.endsWith("Everyone");
        System.out.println(var2);
    }
}

Output:

False

Explanation:

In the second example, we check whether the given string str ends with “Everyone”. Since str does not contain the required sequence of characters, hence the result is false, stored in boolean variable var2.

Example 3

Code:

class Ninja3 {
    public static void main(String args[])
    {
        String str = "HelloWorld"; 
        boolean var3 = str.endsWith("");
        System.out.println(var3);
    }
}

Output:

True

Explanation:

In the third example, we check whether the given string str ends with “”. Since every string ends with “”, hence the result is true, stored in the boolean variable var3.

Example 4

Code:

class Ninja4{    
public static void main(String args[])   
  { 
   	String str = "HelloWorld";  
   	boolean var4 = string.endsWith(“helloWorld”);
   	System.out.println(var4);
  }
}  

Output:

False

Explanation

In the fourth example, we check whether the given string str ends with “helloworld”. Since h and H are not the same, hence the result is false, stored in the boolean variable var4.

Example 5

Code:

class Ninja5{   
	public static void main(String args[])  
	{  
		String str = "HelloWorld";  
		if(str.endsWith(null))  
		{  
			System.out.println("Inside the if block");  
		}  
		else  
		{  
			System.out.println("Inside the else block");  
		}  
	}  
}  

Output:

Exception in thread "main" java.lang.NullPointerException
at java.base/java.lang.String.endsWith(String.java:1485)
at EndsWithExample5.main(EndsWithExample5.java:9)

Explanation:

In the fifth example, we check whether the given string str ends with null. The output gives a null pointer exception as given in the above section.

Frequently Asked Questions

What does endsWith() do in Java?

The endsWith() method checks whether a string ends with the specified character(s).

How do you know if a string ends with certain characters?

prototype.endsWith() method determines whether a string ends with the characters of a specified string, returning true or false as appropriate.

How can you check whether a string starts with a certain character?

We can use the startsWith() method to check whether a string starts with the specified characters.

Conclusion

In this article, we have extensively discussed the concept of the string endsWith() method in Java with the help of code examples. Having gone through this article, I am sure you must be excited to read similar blogs. Coding Ninjas has got you covered. Here are some similar blogs to redirect: Converting Java strings to intUnderstanding strings in javaMethod to take input in javaint to string conversion in java  We hope that this blog has helped you enhance your knowledge, and if you wish to learn more, check out our Coding Ninjas Blog site and visit our Library. Here are some courses provided by Coding Ninjas: Basics of C++ with DSACompetitive Programming and MERN Stack Web Development. Do upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass