Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

String.endsWith()

Checks whether a string ends with the given suffix.

Available since: Java 1.0

Syntax

public boolean endsWith(String suffix)

Parameters

ParameterTypeDescription
suffixStringThe 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
true

Notes

The check is case-sensitive, so it is handy to lowercase file extensions first.

See also

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.

Thanks for reading!

Donate to the site — we work hard for you.