Languages
[Edit]
EN

Java - remove last n characters from string

0 points
Created by:
Payne
654

In this article, we would like to show you how to remove the last n characters from the string in Java.

Quick solution:

 

Practical examples

Edit

1. Using String substring() method

Edit

In this example, we use String substring() method to create a new result substring from the text string without the last n = 3 characters.

Syntax:

Practical example:

Output:

2. Using StringBuilder delete() method

Edit

In this example, we create sb StringBuilder object from the text string, then we use delete() method on the sb to delete the last n = 3 characters. 

Syntax:

Practical example:

Output:

3. Using StringBuffer delete() method 

Edit

In this example, we create sb StringBuffer object from the text string, then we use delete() method on the sb to delete the last n = 3  characters. 

Syntax:

Practical example:

Output:

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 n 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