Java Data Type How to - Remove the last character from a string








Question

We would like to know how to remove the last character from a string.

Answer

/*from   w  w  w  .j a v  a 2s .c o m*/
public class Main {
  public static void main(String[] args) throws java.lang.Exception {
    String s1 = "Remove Last CharacterY";
    String s2 = "Remove Last Character2";
    System.out.println("After removing s1==" + removeLastChar(s1) + "==");
    System.out.println("After removing s2==" + removeLastChar(s2) + "==");

  }

  private static String removeLastChar(String str) {
    return str.substring(0, str.length() - 1);
  }
}