Remove 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: 2020-02-26T05:07:49+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements