Languages
[Edit]
EN

Java - Remove last character from string builder in java

9 points
Created by:
Wallace
636

Remove last character from StringBuilder in Java is simple. StringBuilder has build in method deleteCharAt(). As a parameter we pass current length of our StringBuilder.

Syntax:

1. Remove last char

Edit

2. Safe way to remove last char

Edit

In order to saftly remove last character from StringBuilder in java, we need to check the length of StringBuilder. In case we won't do it and the StringBuilder will be longer we get:

java.lang.StringIndexOutOfBoundsException

Example with safe way of how to remove last char:

This method allow us to avoid the below exception:

3. Remove last char if it is equal to specific character

Edit

This example extends the previous one. We just add one more condition. We need to check if the last letter of StringBuilder is equals to the letter we want to find.

References

Edit
  1. StringBuilder - oracle docs
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - StringBuilder class

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join