.endsWith()

The .endsWith() method returns true if a string ends with a given suffix, otherwise false.

Syntax

str.endsWith(String suffix)
  • suffix (required): The characters to be matched in the string.

Example

The example below demonstrates the use of the .endsWith() method:

// Example.java
class Example {
public static void main(String args[]) {
String domain = "codecademy.com";
// Output will be true as "codecademy.com" ends in "com"
boolean bool = domain.endsWith("com");
System.out.println(bool);
}
}

This outputs the following:

true

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Java on Codecademy