Do you think IIT Guwahati certified course can help you in your career?
No
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.