Languages
[Edit]
EN

Java - remove last 2 characters from string

1 points
Created by:
Root-ssh
178260

1. Overview

Edit

In this post, we will see how to remove the last 2 characters from any String in java.

In the below examples, we can change the number of characters we want to remove.

We can remove the last n characters. For example, we can remove the last character, last 3, 4, 5 x characters. We just need to ensure that the string is long enough. 

Example:

2. Remove char with String.substring()

Edit

Code example: 

Output:

3. Remove char with StringBuilder.delete()

Edit

Code example: 

Output:

4. Remove char with Java 8 Optional

Edit

Code example: 

Output:

5. Remove char with Apache Commons - StringUtils.substring()

Edit

Code example: 

Output:

Merged questions

Edit
  1. Java remove last n characters from string
  2. How to remove last 2 chars in java?
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 - String (popular problems)

Java - remove last 2 characters from string
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