Java String: endsWith() Method
public boolean endsWith(String suffix)
The endsWith() method is used to check if a specified string ends with the specified suffix.
Java Platform: Java SE 8
Syntax:
endsWith(String suffix)
Parameters:
Name | Description | Type |
---|---|---|
suffix | the suffix. | boolean |
Return Value: true if the character sequence represented by the argument is a suffix of the character sequence represented by this object; false otherwise.
Note that the result will be true if the argument is the empty string or is equal to this String object as determined by the equals(Object) method.
Return Value Type: boolean
Pictorial presentation of Java String endsWith() Method
Example: Java String endsWith() Method
The following example shows the usage of java String() method.
public class Example {
public static void main(String[] args)
{
String str1 = "Python Exercises";
String str2 = "Python Exercise";
// The String to check the above two Strings to see
// if they end with this value (se).
String end_str = "se";
// Check first two Strings end with end_str
boolean ends1 = str1.endsWith(end_str);
boolean ends2 = str2.endsWith(end_str);
System.out.println();
// Display the results of the endsWith calls.
System.out.println("\"" + str1 + "\" ends with " +
"\"" + end_str + "\"? " + ends1);
System.out.println("\"" + str2 + "\" ends with " +
"\"" + end_str + "\"? " + ends2);
System.out.println();
}
}
Output:
"Python Exercises" ends with "se"? false "Python Exercise" ends with "se"? true
Java Code Editor:
Previous:copyValueOf Method
Next:equals Method
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/java-tutorial/string/string_endswith.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics