1. Overview

In this quick tutorial, we’re going to explore different techniques for removing the last character of a String.

2. Using String.substring()

The easiest way is to use the built-in substring() method of the String class.

In order to remove the last character of a given String, we have to use two parameters: 0 as the starting index, and the index of the penultimate character. We can achieve that by calling String‘s length() method, and subtracting 1 from the result.

However, this method isn’t null-safe, and if we use an empty string, this is going to fail.

To overcome issues with null and empty strings, we can wrap the method in a helper class:

public static String removeLastChar(String s) {
    return (s == null || s.length() == 0)
      ? null 
      : (s.substring(0, s.length() - 1));
}

We can refactor the code, and use Java 8:

public static String removeLastCharOptional(String s) {
    return Optional.ofNullable(s)
      .filter(str -> str.length() != 0)
      .map(str -> str.substring(0, str.length() - 1))
      .orElse(s);
    }

3. Using StringUtils.substring()

Instead of reinventing the wheel, we can use the StringUtils class from the Apache Commons Lang3 library, which offers helpful String operations. One of them is a null-safe substring() method, which handles exceptions.

To include StringUtils, we have to update our pom.xml file:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

StringUtils.substring() requires three parameters: a given String, an index of the first character (in our case it will be 0 always), and the index of the penultimate character. Again, we can simply use the length() method and subtract 1:

String TEST_STRING = "abcdef";

StringUtils.substring(TEST_STRING, 0, TEST_STRING.length() - 1);

Yet again, this operation isn’t null-safe. It’ll work fine with empty Strings though.

4. Using StringUtils.chop()

The StringUtils class provides the chop() method, which works well with all edge scenarios: empty and null Strings.

It’s very easy to use, and requires only one parameter: the String. Its sole purpose is to remove the last character, nothing more, nothing less:

StringUtils.chop(TEST_STRING);

5. Using Regular Expression

We can also remove the last character (or any number of characters) from a String by making good use of regular expressions.

For example, we can use the replaceAll() method of the String class itself, which takes two parameters: the regular expression and the replacement String:

TEST_STRING.replaceAll(".$", "");

Note that, because we’re calling a method on the String, the operation isn’t null-safe.

Also, replaceAll() and regex expressions can be complex at first sight. We can read more about regex here, but to make the logic a bit more user-friendly, we can wrap it in a helper class:

public static String removeLastCharRegex(String s) {
    return (s == null) ? null : s.replaceAll(".$", "");
}

Note that if a String ends with a newline, then the above method will fail as “.” in regex matches for any character except for line terminators.

Finally, let’s re-write the implementation with Java 8:

public static String removeLastCharRegexOptional(String s) {
    return Optional.ofNullable(s)
      .map(str -> str.replaceAll(".$", ""))
      .orElse(s);
}

6. Conclusion

In this brief article, we discussed different ways of removing only the last character of a String, some manual and some ready out of the box.

If we need more flexibility, and we need to remove more characters, we can use the more advanced solution with regular expressions.

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