How to remove the last character from a string

0 votes

Hi Team,

I am new to Java. I want to remove the last character from a string. How can I do that?

Dec 30, 2020 in Java by akhtar
• 38,260 points
656 views

1 answer to this question.

0 votes

Hi@akhtar,

You can use the StringBuffer class. It provides a method deleteChartAt(). The method deletes a character from the specified position. We use the method to remove a character from a string Java. It accepts a parameter index of type int. The index is the position of a character we want to delete.

public class RemoveLastCharcter1  
{  
public static void main(String args[])  
{  
String string = "I am learning Java Concepts";  
//creating a constructor of StringBuffer class  
StringBuffer sb= new StringBuffer(string);  
//invoking the method  
sb.deleteCharAt(sb.length()-1);  
//prints the string after deleting the character   
System.out.println(sb);  
}  
} 
answered Dec 30, 2020 by MD
• 95,460 points

Related Questions In Java

0 votes
1 answer

How to remove ‘char’ from a String?

Hi...here you can try using the overloaded ...READ MORE

answered May 22, 2018 in Java by code.reaper12
• 3,500 points
693 views
0 votes
1 answer

How do I create a Java string from the contents of a file?

If you're looking for an alternative that ...READ MORE

answered Apr 19, 2018 in Java by Rishabh
• 3,620 points
1,178 views
0 votes
2 answers

How to find out a single character appears in String or not in Java?

You can use string.indexOf('s'). If the 's' is present in string, ...READ MORE

answered Aug 7, 2018 in Java by Sushmita
• 6,920 points
5,716 views
0 votes
1 answer

How can we remove html tags from a string?

You can use HTML parser. This is ...READ MORE

answered Jun 4, 2018 in Java by sophia
• 1,400 points
4,190 views
0 votes
2 answers

Store String inside a File using Java

We can use Apache Commons IO. It ...READ MORE

answered Jul 20, 2018 in Java by Sushmita
• 6,920 points
1,409 views
0 votes
3 answers

Adding text to a file using Java

try { final Path ...READ MORE

answered Sep 6, 2018 in Java by Sushmita
• 6,920 points
1,413 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
1,326 views
0 votes
1 answer

How to split Strings by space in Java ?

You can use split() method. str = "Hello ...READ MORE

answered May 16, 2018 in Java by sharth
• 3,370 points
1,572 views
0 votes
1 answer

How do I check if a string contains a substring in Java?

Hi@akhtar, The first and foremost way to check ...READ MORE

answered Dec 30, 2020 in Java by MD
• 95,460 points

edited Jul 5, 2023 by Khan Sarfaraz 56,661 views
0 votes
1 answer

How to Install Java in Windows system?

Hi@akhtar, You can follow the below-given steps to install ...READ MORE

answered Jul 7, 2020 in Java by MD
• 95,460 points
6,446 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP