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,170 points
91 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,060 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 21, 2018 in Java by code.reaper12
• 3,500 points
151 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
380 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,890 points
1,176 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
823 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,890 points
158 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,890 points
247 views
0 votes
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
278 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,350 points
294 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,060 points
129 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 6, 2020 in Java by MD
• 95,060 points
1,095 views