Java String endsWith() Method with Examples
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
The String class in Java contains many useful methods for manipulating and comparing strings. One of these handy methods is the endsWith() method, which allows you to check if a string ends with a specified suffix or character sequence. With just one line of code, endsWith() can tell you whether the ending of a string matches what you expect.
In this easy-to-follow guide for beginners, we’ll explore Java’s endsWith() method. We’ll learn how it works, its basic structure, and when to use it, and we’ll see some practical examples. By the end, you’ll know how to use endsWith() to effectively check the endings of strings in your Java code.
How Java endsWith() Works
The Java endsWith() method checks if a string ends with the specified suffix and returns a boolean value indicating the result:
- Returns true if the string does end with the given suffix
- Returns false if the string does not end with the given suffix
The syntax for using endsWith() is:
boolean result = string.endsWith(suffix);
Where string is the original string that you want to check, and the suffix is the ending sequence you want to test for.
Some things to note about endsWith():
- It is case-sensitive – “Hello”.endsWith(“lo”) will return true, while “Hello”.endsWith(“LO”) will return false
- The comparison is done character by character, starting from the end of the original string
- endsWith() will return true if the suffix matches a sequence of characters at the end of the string, even if the suffix is not a full word
Now let’s look at some examples to understand the use cases of endsWith().
Examples of Using Java endsWith()
Example 1:
public class EndsWithExample {
public static void main(String[] args) {
String str1 = "Hello World";
// Check if str1 ends with "World"
boolean result1 = str1.endsWith("World"); // Returns true
System.out.println("Does str1 end with 'World'? " + result1);
// Check if str1 ends with "Earth"
boolean result2 = str1.endsWith("Earth"); // Returns false
System.out.println("Does str1 end with 'Earth'? " + result2);
// Check if str1 ends with "ld"
boolean result3 = str1.endsWith("ld"); // Returns true
System.out.println("Does str1 end with 'ld'? " + result3);
}
}Output:
Does str1 end with ‘World’? true
Does str1 end with ‘Earth’? false
Does str1 end with ‘ld’? true
In the above example, result1 is true because “World” exactly matches the last 6 characters of str1.
result2 is false because “Earth” does not match the ending sequence of str1.
And result3 is true because “ld” matches the last 2 characters of str1.
Example 2:
public class EndsWithExample2 {
public static void main(String[] args) {
String str2 = "This is a test";
// Check if str2 ends with "test"
boolean result4 = str2.endsWith("test"); // Returns true
System.out.println("Does str2 end with 'test'? " + result4);
// Check ending with a single character
boolean result5 = str2.endsWith("t"); // Returns true
System.out.println("Does str2 end with 't'? " + result5);
// Check ending with an empty string
boolean result6 = str2.endsWith(""); // Returns true
System.out.println("Does str2 end with an empty string? " + result6);
}
}Output:
Does str2 end with ‘test’? true
Does str2 end with ‘t’? true
Does str2 end with an empty string? true
Here, we can see that endsWith() can match a complete word, a single character, or even an empty string. The empty string test will always return true.
Example3:
public class EndsWithExample3 {
public static void main(String[] args) {
String str3 = "HelloWorld";
// Check if str3 ends with "WORLD" (case-sensitive)
boolean result7 = str3.endsWith("WORLD"); // Returns false
System.out.println("Does str3 end with 'WORLD'? " + result7);
// Check if str3 ends with "orld"
boolean result8 = str3.endsWith("orld"); // Returns true
System.out.println("Does str3 end with 'orld'? " + result8);
}
}Output:
Does str3 end with ‘WORLD’? false
Does str3 end with ‘orld’? true
Since endsWith() is case-sensitive, result7 returns false even though “WORLD” matches the ending sequence ignoring casing. result8 shows that the proper casing “orld” matches.
Example4:
public class EndsWithExample4 {
public static void main(String[] args) {
String str4 = "Hello World ";
// Check if str4 ends with a space
boolean result9 = str4.endsWith(" "); // Returns true
System.out.println("Does str4 end with a space? " + result9);
}
}Output:
Does str4 end with a space? true
Here endsWith(” “) allows us to detect the trailing whitespace at the end of str4 easily.
Conclusion
In conclusion, this guide has covered the ins and outs of using the endsWith() method in Java – how it checks for a matching suffix and returns true or false, the need to match casing, its ability to match words, characters or empty strings, checking for whitespace, and avoiding null parameters.
Armed with this in-depth understanding of endsWith(), you should now feel confident incorporating it into your own code to validate string endings and input, taking advantage of the many use cases endsWith() provides for string manipulation in Java.
Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

