Check if a String Ends with a Certain Pattern in Java – 在Java中检查一个字符串是否以某种模式结束

最后修改: 2022年 1月 8日

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

2.2.String#matches 方法

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.

matches 是另一个伟大的方法,我们可以用它来实现我们的目标。它简单地检查一个字符串是否与给定的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

4.使用Apache Commons Lang

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

Apache Commons Lang提供了一组用于字符串操作的即用型实用类。在这些类中,我们发现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.

首先,我们看到了使用内置的Java类来实现这一目标的几种方法。然后,我们解释了如何使用Apache Commons Lang库来做同样的事情。

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