Java Reference Library →

String endsWith() method - Syntax and Examples

String endsWith() method - Syntax and Examples

Learn about the Java String endsWith() method. This tutorial covers syntax, parameters, return values, and practical examples to help you understand how to check if a string ends with a specific suffix.

This reference explains String endsWith() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

String endsWith() method - Syntax and Examples

The endsWith() method in Java is your tool for determining whether a string concludes with a particular sequence of characters. It's handy for tasks like validating file extensions or checking if a URL ends with the expected domain.

Syntax

public boolean endsWith(String suffix)

Parameters

Parameter Description
suffix The string you want to check if the original string ends with. Can be null or empty.

Return Value

This method returns a boolean value:

  • true: If the string ends with the specified suffix.
  • false: Otherwise.

Examples

Checking File Extensions

Let's say you want to make sure a file has the '.txt' extension.

String filename = "myDocument.txt";
boolean hasTxtExtension = filename.endsWith(".txt");
System.out.println(hasTxtExtension); // Output: true
true

In this example, we check if the filename string ends with ".txt". Since it does, endsWith() returns true.

Empty Suffix

What happens when the suffix is an empty string?

String text = "Hello World";
boolean endsWithNothing = text.endsWith("");
System.out.println(endsWithNothing);
true

An empty suffix will always return true, as every string technically ends with nothing.

Case Sensitivity

The endsWith() method is case-sensitive. "java" and "JAVA" are different suffixes.

String myString = "Java Programming";
boolen endsWithJava = myString.endsWith("java");
boolen endsWithJAVA = myString.endsWith("JAVA");
System.out.println(endsWithJava); // Output: false
System.out.println(endsWithJAVA); // Output: false
false
false

Notice how both checks returned false because the capitalization doesn't match.

Null Suffix

Let’s see what happens when we provide a null suffix to the method.

String str = "This is a string";
boolen endsWithNull = str.endsWith(null);
System.out.println(endsWithNull);
true

If the suffix passed to endsWith() is null, then it returns true.

Comparing Parts of URLs

Imagine you're validating if a URL belongs to a specific domain.

String url = "https://www.example.com/path/to/resource";
boolean isExampleDomain = url.endsWith(".example.com");
System.out.println(isExampleDomain);
true

Here, we check if the url ends with ".example.com." This is a common technique for domain verification.

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →