In this article you will learn how to remove last characters from a String. Beside of the Java standard API calls you will learn also other alternatives.

1. Standard Java API

Java in his early days in the 90s had in the market a very good reputation about his good and easy to use API.

1.1. The only way up to Java 7 with String.substring()

Java's built-in method substring() of the class String is the most known way of how to remove the last character. This is done by getting from the existing String all characters starting from the first index position 0 till the next to last position, means the length of the string minus 1. Also you have to know that String.substring() is not null-safe and you have to handle that corner case on your own.

public static String removeLastCharacter(String str) {
String result = null;
if ((str != null) && (str.length() > 0)) {
result = str.substring(0, str.length() - 1);
}
return result;
}

This code will also work in any later Java version above Version 7.

1.2. Since Java 8 with the new Optional util class

With Java 8 Oracle introduced a new utiltiy class to handle null references more effectively. We now refactor our pre Java 8 code into Java 8 Optional code.

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

Consider on your own, which code variant your prefer most of both. But wait till you see the more fancy way...

2. Other libs

Since the time evolved but sadly the Java API did not get more helpful methods for handle more specifical requirements about String, Apache commons Lang (formerly Jakarta Commons Lang) added a utility class StringUtils. This class has multiple option to handle strings.

2.1. Apache Commons Lang StringUtils.chop()

This method helps you in the very specific requirement to remove the last character or also named to chop of.

public static String removeLastCharacter(String str) {
   String result = StringUtils.chop(str);
   return result;
}

This code is basically only a one liner StringUtils.chop(str), which is null-safe and also works with empty strings well.

For having the class StringUtils in your classpath, you will need to add to your maven build the dependency to your build.

<dependencies>
...some other deps ...
<dependency>
   <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
...some other deps ...
</dependencies>

2.2. Apache Commons Lang StringUtils.substring()

Already in the early days of Java the Apache Commons (formerly Jakarta Commons) have already improved the substring() method. The StringUtils.substring() is null-safe along with stable to empty strings. The method signature is nearly the same as of the String.substring() method. The only have to pass also the reference to our String.

public static String removeLastCharacter(String str) {
String result = StringUtils.substring(str, 0, str.length() - 1);
return result;
}

Also here you have to add the maven dependency, like mentioned with the chop example.

3. Finally, which one to use?

This depends on your project needs. If you already have the Apache commons Lang integrated use the StringUtils, it is pretty handy and you gain a lot of more String operations for free. If you - in really seldom cases - cannot use the Apache commons Lang you have to stay on the Java API. The Java API is also okay but of course not so handy to use.

More to read here: