.endsWith()

JaceInglis's avatar
Published Feb 16, 2023
Contribute to Docs

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

All contributors

Contribute to Docs

Learn Java on Codecademy