Remove the last character of a string in Java

In this quick article, we'll look at different ways to remove the last character of a string in Java.

Using String.substring()

The easiest and quickest way to remove the last character from a string is by using the String.substring() method. Here is an example:

String str = "Hey, there!!";

// remove last character (!)
str = str.substring(0, str.length() -1);

// print the new string

As you can see above, we are using substring() with two parameters. The first parameter is the starting inclusive index which is 0 in our case. The second parameter is basically the ending exclusive index, calculated by taking the length of the string using the length() method and by subtracting 1 from the length.

Now if you execute the code, you should see the following output:

Hey, there!

However, there is a minor issue. The substring() method is not null-safe, which means it will through an exception if the string is null or empty. To avoid the exception, we have to explicitly check the length of the string as shown in the below example:

str = str != null && !str.isEmpty() ? str.substring(0, str.length() - 1) : null;

If you prefer to use Java 8 streams, just do the following:

str = Optional.ofNullable(str)
        .filter(s -> !s.isEmpty())
        .map(s -> s.substring(0, s.length() - 1))

Using Regular Expression

Another way to remove the last character from a string is by using a regular expression with the replaceAll() method. This method replaces all occurrences of the matched string with the given string.

The replaceAll() method takes two input parameters: the regular expression and the replacement string. Here is an example code snippet that removes the last character of the string using replaceAll():

String str = "Hello World!";

// remove last character (!)
str = str.replaceAll(".$", "");

// print the new string

Since replaceAll() is a part of the String class, it is not null-safe. So we have to do a little more work:

str = str != null && !str.isEmpty() ? str.replaceAll(".$", "") : null;

The replaceAll() method compiles the regular expression every time it executes. To avoid this unnecessary compilation, you should use the Pattern class to compile the regular expression into a patternjava:

Pattern pattern = Pattern.compile(".$");
str = str != null && !str.isEmpty() ?
        pattern.matcher(str).replaceAll("") :

The equivalent example that uses Java 8 streams to remove the last character of the string:

str = Optional.ofNullable(str)
        .map(s -> s.replaceAll(".$", ""))

Apache Commons Lang

The Apache Commons Lang library is a popular open-source library that provides many utility classes. One such class is the StringUtils that offers useful utility methods for string operations.

To add the library to your Maven project, add the following dependency to pom.xml file:


For Gradle, add the below dependency to your build.gradle file:

implementation 'org.apache.commons:commons-lang3:3.9'

To remove the class character from a string, just use the StringUtils.substring() method. This method is null-safe, which means it won't throw an exception if the string is null:

String str = "Hello World!";

// remove last character (!)
StringUtils.substring(str, 0, str.length() - 1);

// print the new string

The StringUtils.substring() method takes one extra parameter than the built-in substring(); the string you want to remove a character from.

Read Next: How to extract digits from a string in Java

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.