Java String endsWith() Method Example

The String.endsWith() method in Java is used to check if a 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.

Table of Contents

  1. Introduction
  2. endsWith Method Syntax
  3. Examples
    • Basic Example
    • Case Sensitivity
    • Checking Multiple Suffixes
  4. Conclusion

Introduction

The String.endsWith() method is part of the String class in Java. It allows you to determine whether a given string ends with a specified suffix. This is useful in various scenarios, such as file extension validation, URL checking, or string pattern matching.

endsWith Method Syntax

The syntax for the endsWith method is as follows:

public boolean endsWith(String suffix)
  • suffix: The suffix to be checked.
  • Returns: true if the character sequence represented by the argument is a suffix of the character sequence represented by this string; false otherwise.

Examples

Basic Example

In this example, we'll use the endsWith method to check if a string ends with a specified suffix.

Example

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

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

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

Output:

Does the string end with 'World!'? true

Case Sensitivity

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

Example

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

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

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

Output:

Does the string end with 'world!'? false

Checking Multiple Suffixes

You can use the endsWith method to check if a string ends with any of several specified suffixes.

Example

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

        // Check if the file name ends with ".pdf" or ".doc"
        boolean endsWithPdf = fileName.endsWith(".pdf");
        boolean endsWithDoc = fileName.endsWith(".doc");

        System.out.println("Does the file name end with '.pdf'? " + endsWithPdf);
        System.out.println("Does the file name end with '.doc'? " + endsWithDoc);
    }
}

Output:

Does the file name end with '.pdf'? true
Does the file name end with '.doc'? false

Handling Null Suffix

If the suffix is null, the endsWith method will throw a NullPointerException.

Example

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

        try {
            // Check if the string ends with null
            boolean result = str.endsWith(null);
        } catch (NullPointerException e) {
            System.out.println("NullPointerException: " + e.getMessage());
        }
    }
}

Output:

NullPointerException: Cannot invoke "String.length()" because "suffix" is null

Conclusion

The String.endsWith() method in Java provides a way to check if a string ends with a specified suffix. This method is case-sensitive and is useful for various string pattern matching scenarios. By understanding how to use this method, you can effectively perform suffix checks in your Java applications.


Comments