Open In App

Java String endsWith() with Examples

Last Updated : 20 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Java
// Java program to demonstrate endsWith() method

public class StringEndsWith {

    public static void main(String[] args) {

        // Declare and initialize a String
        String s = "Java Programming";

        // Check if the string ends with "Programming"
        boolean r = s.endsWith("Programming");

        System.out.println("" + r);
    }
}

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:

Java
// Java program to demonstrate case sensitivity of endsWith()

public class StringEndsWith {

    public static void main(String[] args) {

        // Declare and initialize a String
        String s = "Java Programming";

        // Check if the string ends with "programming" (lowercase)
        boolean r = s.endsWith("programming");

        System.out.println("" + r);
    }
}

Output
false

2. Checking with a Different Suffix

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

Example:

Java
// Java program to demonstrate endsWith() with a different suffix

public class StringEndsWith {

    public static void main(String[] args) {

        // Declare and initialize a String
        String s = "Java Programming";

        // Check if the string ends with "Programming"
        boolean r = s.endsWith("Programming");

        System.out.println("" + r);
    }
}

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:

Java
// Java program to demonstrate endsWith() with an empty string

import java.util.*;

public class StringEndsWith {

    public static void main(String[] args) {

        // Declare and initialize a String
        String s = "Java Programming";

        // Check if the string ends with an empty string
        boolean r = s.endsWith("");

        System.out.println("" + r);
    }
}

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:

Java
// Java program to demonstrate endsWith() with file extension
import java.util.*;

public class StringEndsWith {

    public static void main(String[] args) {

        // Declare and initialize a String representing file name
        String fileName = "document.txt";

        // Check if the file name ends with ".txt"
        boolean r = fileName.endsWith(".txt");

        System.out.println("" + r);
    }
}

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:

Java
// Java program to demonstrate the working of endsWith() method

class StringEndsWith {

    public static void main(String args[]) {
      
        String s = "Java Programming";

        // Output will be false if the argument
        // is the empty space
        boolean b = s.endsWith(" ");
        System.out.println(b);
    }
}

Output
false



Practice Tags :

Similar Reads