Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this short tutorial, we’re going to cover in-depth how to check if a string ends with a certain pattern in Java.

First, we’ll start by considering solutions using core Java. Then, we’re going to showcase how to accomplish the same thing using external libraries.

2. Using the String Class

Simply put, String provides multiple convenient options to verify if a given string ends with a particular substring.

Let’s take a closer look at each option.

2.1. String#endsWith Method

This method is introduced typically for this purpose. It offers the most straightforward way to check if a String object ends with another string.

So, let’s see it in action:

public static boolean usingStringEndsWithMethod(String text, String suffix) {
    if (text == null || suffix == null) {
        return false;
    }
    return text.endsWith(suffix);
}

Please note that endsWith is not null-safe. So, we need first to make sure that text and suffix are not null to avoid NullPointerException.

2.2. String#matches Method

matches is another great method that we can use to achieve our objective. It simply checks whether or not a string matches a given regular expression.

Basically, all we need to do is specify the regex that works properly for our use case:

public static boolean usingStringMatchesMethod(String text, String suffix) {
    if (text == null || suffix == null) {
        return false;
    }
    String regex = ".*" + suffix + "$";
    return text.matches(regex);
}

As we can see, we used a regular expression that matches suffix at the end ($) of our string text. Then, we passed the regex to the matches method.

2.3. String#regionMatches Method

Similarly, we can use regionMatches method to address our central question. It returns true if a string part matches exactly the specified string, and it returns false otherwise.

Now, let’s illustrate this with an example:

public static boolean usingStringRegionMatchesMethod(String text, String suffix) {
    if (text == null || suffix == null) {
        return false;
    }
    int toffset = text.length() - suffix.length();
    return text.regionMatches(toffset, suffix, 0, suffix.length());
}

toffset denotes the starting offset of the subregion in our string. So, in order to check if text ends with the specified suffix, toffset should be equal to the length of text minus the length of suffix.

3. Using the Pattern Class

Alternatively, we can use the Pattern class to compile a regular expression that checks if a string ends with a pattern.

Without further ado, let’s reuse the same regex we specified in the previous section:

public static boolean usingPatternClass(String text, String suffix) {
    if (text == null || suffix == null) {
        return false;
    }
    Pattern pattern = Pattern.compile(".*" + suffix + "$");
    return pattern.matcher(text).find();
}

As shown above, Pattern compiles the previous regex, which denotes the end of a string, and attempts to match it against our string text.

4. Using Apache Commons Lang

Apache Commons Lang provides a set of ready-to-use utility classes for string manipulation. Among these classes, we find StringUtils.

This utility class comes with an interesting method called endsWith. It checks if a sequence of characters ends with a suffix in a null-safe manner.

Now, let’s exemplify the use of the StringUtils.endsWith method:

public static boolean usingApacheCommonsLang(String text, String suffix) {
    return StringUtils.endsWith(text, suffix);
}

5. Conclusion

In this article, we’ve explored different ways to check if a string ends with a particular pattern.

First, we saw a couple of ways to achieve this using built-in Java classes. Then, we explained how to do the same thing using the Apache Commons Lang library.

As always, the code used in this article can be found over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.