Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


1. Overview

When we want to build a string in Java, we usually choose the convenient StringBuilder to do the job.

Suppose we have a StringBuilder sequence containing some string segments and we want to remove the last character from it. In this quick tutorial, we'll explore three ways to do this.

2. Using StringBuilder‘s deleteCharAt() Method

The StringBuilder class has the deleteCharAt() method. It allows us to remove the character at the desired position.

The deleteCharAt() method has only one argument: the char index we want to delete.

Therefore, if we pass the last character's index to the method, we can remove the character. For simplicity, we'll use unit test assertions to verify it works as expected.

So next, let's create a test to check if it works:

StringBuilder sb = new StringBuilder("Using the sb.deleteCharAt() method!");
sb.deleteCharAt(sb.length() - 1);
assertEquals("Using the sb.deleteCharAt() method", sb.toString());

As the test above shows, we pass the last character's index (sb.length() -1 ) to the deleteCharAt() method, and expect the ending exclamation mark (!) to be removed.

If we run the test, it passes. So, deleteCharAt() solves the problem.

3. Using StringBuilder‘s replace() Method

StringBuilder‘s replace() method allows us to replace characters in a substring of the sequence with the given string. The method accepts three parameters:

  • start index – the beginning index, inclusive
  • end index – the ending index, exclusive
  • replacement – the string used to replace

Let's say the last character's index in the sequence is lastIdx. If we want to remove the last character, we can pass lastIdx as the start index, lastIdx+1 as the end index, and an empty string “” as the replacement to replace():

StringBuilder sb = new StringBuilder("Using the sb.replace() method!");
int last = sb.length() - 1;
sb.replace(last, last + 1, "");
assertEquals("Using the sb.replace() method", sb.toString());

Now, the test above passes if we give it a run. So, the replace() method can be used to solve the problem.

4. Using StringBuilder‘s substring() Method

We can use StringBuilder‘s substring() method to get a subsequence from the given start and end indexes of the string. The method requires two arguments, the beginning index (inclusive) and the ending index (exclusive).

It's worth mentioning that the substring() method returns a new String object. In other words, the substring() method doesn't modify the StringBuilder object.

We can pass 0 as the start index and the last character's index as the end index to the substring() method to get a string with the last character chopped off:

StringBuilder sb = new StringBuilder("Using the sb.substring() method!");
assertEquals("Using the sb.substring() method", sb.substring(0, sb.length() - 1));
//the stringBuilder object is not changed
assertEquals("Using the sb.substring() method!", sb.toString());

The test passes if we execute it.

As we can see in the test, even though the String returned by the substring() is without the last character (!), the original StringBuilder isn't changed.

5. Conclusion

In this brief article, we've learned how to remove the last character from a StringBuilder sequence.

As usual, all code snippets presented here are available over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Generic footer banner
Inline Feedbacks
View all comments