How to remove the last character from a string in Java?


The StringBuffer class contains a method known as deleteCharAt(). This method deletes the character at a specified index/position. You can use this method to delete/remove a particular character from a string in Java.

Example

public class Test {
   public static void main(String args[]){
      String str = "hi welcome to Tutorialspoint";
      StringBuffer sb= new StringBuffer(str);
      sb.deleteCharAt(sb.length()-1);
      System.out.println(sb);
   }
}

Output

hi welcome to Tutorialspoint

Updated on: 26-Feb-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements