String.endsWith()
Checks whether a string ends with the given suffix.
Available since: Java 1.0
Syntax
public boolean endsWith(String suffix)Parameters
| Parameter | Type | Description |
|---|---|---|
| suffix | String | The expected end of the string. |
Return value
booleantrue if the string ends with suffix.
Examples
Checking a file extension:
void main() {
String file = "photo.JPG";
IO.println(file.endsWith(".jpg"));
IO.println(file.toLowerCase().endsWith(".jpg"));
}Output:
false
trueNotes
The check is case-sensitive, so it is handy to lowercase file extensions first.
See also
- String.startsWith() — Checks whether a string starts with the given prefix.
- String.lastIndexOf() — Finds the position of the last occurrence of a character or a substring.
Related recipes
In the tutorial
- String methods — The main String methods in Java: searching, substring, changing case, strip, replace, split, join, repeat and string checks.