VOOZH about

URL: https://www.geeksforgeeks.org/java/java-string-endswith-examples/

⇱ Java String endsWith() with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java String endsWith() with Examples

Last Updated : 11 Jul, 2025

In Java, the endsWith() method of the String class is used to check if a string ends with a specific suffix. The endsWith() method is present in the java.lang package. In this article, we will learn how to use the endsWith() method in Java and explore its practical examples.

Example:

In this example, we will use the endsWith() method to check whether the string ends with a specific suffix.


Output
true

Syntax of endsWith() method

boolean endsWith(String suffix)

Parameter:

  • suffix: The suffix to be checked.

Return Type:

  • If the string ends with the given suff, it returns true.
  • If the string does not end with the stuff, it returns false.

Other Examples of String endsWith() Method

1. Case Sensitivity Example

The endsWith() method is case-sensitive. This means that it will return false if the case of the characters does not match the specified suffix.

Example:


Output
false

2. Checking with a Different Suffix

We can also check whether the string ends with any suffix.

Example:


Output
true

3. Using endsWith() with an Empty String

We can check if a string ends with an empty string or not. It will always return true, because every string ends with an empty string.

Example:


Output
true


4. Using endsWith() for File Extensions

The endsWith() method used for checking file extensions. We can check if a file name ends with .txt or anything else.

Example:


Output
true


5. Check String Ends with a Space

We can also check if a string ends with a space using the endsWith() method.

Example:


Output
false
Comment