Java String endsWith() Method

The String.endsWith() method in Java is used to check if a given string ends with a specified suffix. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality using various strings. We will also cover a real-world use case to show how String.endsWith() can be used effectively.

Table of Contents

  1. Introduction
  2. endsWith Method Syntax
  3. Examples
    • Checking if a String Ends with a Suffix
    • Handling Non-Matching Suffixes
  4. Real-World Use Case
    • Example: Validating File Extensions
  5. Conclusion

Introduction

The String.endsWith() method is a member of the String class in Java. It allows you to check if a given string ends with a specified suffix. If the string ends with the specified suffix, the method returns true; otherwise, it returns false.

endsWith Method Syntax

The syntax for the endsWith method is as follows:

public boolean endsWith(String suffix)
  • Parameters:
    • suffix: The suffix to be checked.
  • Returns: true if the string ends with the specified suffix, false otherwise.

Examples

Checking if a String Ends with a Suffix

The endsWith method can be used to check if a string ends with a specified suffix.

Example

public class EndsWithExample {
    public static void main(String[] args) {
        String str = "Hello, world!";

        // Checking if the string ends with "world!"
        boolean result = str.endsWith("world!");

        // Printing the result
        System.out.println("Does the string end with 'world!'? " + result);
    }
}

Output:

Does the string end with 'world!'? true

Handling Non-Matching Suffixes

If the string does not end with the specified suffix, the endsWith method returns false.

Example

public class NonMatchingSuffixExample {
    public static void main(String[] args) {
        String str = "Hello, world!";

        // Checking if the string ends with "hello"
        boolean result = str.endsWith("hello");

        // Printing the result
        System.out.println("Does the string end with 'hello'? " + result);
    }
}

Output:

Does the string end with 'hello'? false

Real-World Use Case

Example: Validating File Extensions

A common real-world use case for String.endsWith() is validating file extensions to ensure files are of a specific type.

Example

public class FileExtensionValidator {
    public static void main(String[] args) {
        String fileName = "document.pdf";

        // Checking if the file name ends with ".pdf"
        boolean isValidPDF = fileName.endsWith(".pdf");

        // Printing the validation result
        System.out.println("Is the file a PDF? " + isValidPDF);
    }
}

Output:

Is the file a PDF? true

In this example, String.endsWith() is used to validate the file extension of a given file name, ensuring that the file is a PDF.

Conclusion

The String.endsWith() method in Java provides a way to check if a string ends with a specified suffix. By understanding how to use this method, you can efficiently perform suffix checks in your Java applications. The method allows you to validate strings based on their ending characters, making it a versatile tool for various scenarios such as file extension validation and string pattern matching.

Comments