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

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 09-Feb-2018 10:18:41
Advertisements