How to remove last character of a StringBuilder in Java

This example shows how to remove last character of a StringBuilder. There are several ways of doing this but here two possible ways are shown.
The first example uses the method setLength of the StringBuilder to truncate the length by one:

Since the letter ‘g’ is removed from the StringBuilder the output from the code above will be:

Another way of removing last character from a StringBuilder is to use the method deleteCharAt with the last position index as parameter. Like this:

The output from the last example is of course the same as above.

Search for more Java info on this site here:
Custom Search

Please type any questions here.