Java remove last character from string

Previous

In this post, we will see how to remove last character from String in java.

There are many ways to do it. Let’s see each one by one.


Using String.substring()

We can use inbuilt String’s substring method to remove last character.We need two parameters here
start index: 0
end index:str.length()-1

Please note that this method is not thread safe and it will fail with empty or null String.
So here is java program to remove last character from String in java.

Output:

java2blo

Using StringUtils.chop()

You need to add below dependency to get Apache commons lang jar.

and then use StringUtils.chop() method to remove last character from the String in java,

Output:

java2blo

Using Regular Expression

We can make use of Regular expression also to remove last character from String in java.

Output:

java2blo

That’s all about how to remove last character from String in java.

Previous

Add Comment