The endsWith()
method is a built-in method in Java that checks whether a string ends with a specified suffix. It returns a boolean value of true if the string ends with the specified suffix, and false otherwise.
The syntax of the endsWith()
method is as follows:
boolean endsWith(String suffix)
Here, suffix
is the string that is to be checked as the suffix in the original string. This method is case-sensitive.
Example usage:
String str1 = "Hello, World!";
boolean ends1 = str1.endsWith("World!"); // true
String str2 = "Hello, World!";
boolean ends2 = str2.endsWith("world!"); // false (case-sensitive)
String str3 = "Hello, World!";
boolean ends3 = str3.endsWith("ello"); // true (partial match at end)
String str4 = "Hello, World!";
boolean ends4 = str4.endsWith("Hello"); // false (not at the end)
In the above examples, the endsWith()
method is used to check whether the given suffix exists at the end of the original string.