This post will discuss how to remove the last character from the end of a String in Java.

1. Using String.substring() method

To chop the last character from the string’s end, you can simply make a call to the substring() method with the last index excluded. Here’s a utility method demonstrating this:

Download  Run Code

2. Using String.replaceFirst() method

Both replaceFirst() and replaceAll() method accepts regular expression for substring replacement. You can use the regular expression .$ to match with the last character of a string. Here, . matches with any character except a line terminator, and $ matches with the end of a string.

Download  Run Code

3. Using StringUtils.chop() method

This post is incomplete without a solution that doesn’t re-invent the wheels. Apache Commons StringUtils class has a chop() method that removes the last part of a String.

Download Code

 
If you have a specific substring to be removed from the end of a string, you may use the removeEnd() method from the StringUtils class.

Download Code

That’s all about removing the last character from the end of a String in Java.