Java .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.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours

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

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours