Java String Handling

Java String remove last character example

Java String remove last character example shows how to remove last character from String in Java. Example also shows how to remove last 2, 3, and n characters from String.

How to remove last character from String in Java?

Last character of String can be removed using substring method of String class.

This method returns string that is substring of the original String.

Important Note: Remember, startIndex is inclusive while endIndex is exclusive. That is, substring starts at startIndex and goes up to endIndex – 1 (not till the endIndex).

You can remove last character of String using below given approaches.

1) Java String remove last character using substring method

Take substring from the original String from 0 to string length – 1 index to remove the last character from the String as given below.

Output

It is necessary to check for null, otherwise invoking substring method on null String will throw NullPointerException. If string is empty and substring is called, it will throw ArrayIndexOutOfBoundException.

2) Java String remove last character using Apache Commons library

If you are using Apache Commons library, you can use removeEnd method of StringUtils class to remove last character from the String as given below.

Output

How to check the last character before removing it?

Sometimes it is a good idea to check the last character before removing it from the String. You can use endsWith method of String class to do that.

For example, if you want to make sure that the last character of the String is indeed a comma, you can do so as given below.

Output

Using Apache Commons

If you are using removeEnd method of StringUtils class, there is no need to check the last character. That is because, removeEnd method removes the last character only if the string ends with it. If the last character does not match, it returns the original String back as given below.

Output

Since “Hello, world,” string does not end with the “d”, it is not removed from String and original String is returned back.

How to remove last 2 characters from the String?

Use substring method and specify 0 as start index and string length – 2 as end index to remove last 2 characters from the String. Make sure that string’s length is at least 2, otherwise substring method will throw ArrayIndexOutOfBoundException.

Output

How to remove last n characters from the String?

Same as above, take substring from 0 to string length – n to remove last n characters from the String. Make sure the string is at least n characters in length.

Please let us know your views in the comments section below.

Want to learn quickly?
Try one of the many quizzes. I promise you will not be disappointed.

About the author

rahimv

rahimv

rahimv has over 15 years of experience in designing and developing Java applications. His areas of expertise are J2EE and eCommerce. If you like the website, follow him on Facebook, Twitter or Google Plus.

Add Comment

Your email address will not be published. Required fields are marked *