Java StringBuilder Delete Last Character

Java StringBuilder Delete Last Character | There are many scenarios where we have to delete some characters or strings that might be due to some issues. To do this, Java provides a built-in method that makes it for the programmer easy to delete unwanted characters or strings.

In the StringBuilder class, we have the delete method, there are two variations in the delete method, and the syntax for the delete method is as follows:-
1. public StringBuilder delete(int begin, int end)
2. public StringBuilder deleteCharAt(int index)

Parameters:
index: the index where the character needs to be deleted.
begin: specifies the method from where to start the delete operation, inclusive.
end: specifies the method to end the delete operation, exclusive.
Return type: StringBuilder.
Returns: the StringBuilder after deletion.

The delete(int begin, int end) throws StringIndexOutOfBoundsException if “start” is negative, greater than “length()”, or greater than “end”. The deleteCharAt(int index) throws StringIndexOutOfBoundsException if the “index” is negative or greater than or equal to “length()”.

StringBuilder delete() Java Example

The below program deletes the substring “en” in the word “pen” that the index is from 5 to 8.

public class Main {
   public static void main(String[] args) {
      StringBuilder string = new StringBuilder("The pen is mightier than the sword");
      System.out.println("StringBuilder = " + string.toString());
      StringBuilder afterRemoval = string.delete(5, 8);
      System.out.println("After removal = " + afterRemoval);
   }
}

Output:-

StringBuilder = The pen is mightier than the sword
After removal = The pis mightier than the sword

Java StringBuilder Delete Last Character Example

Let us see a Java program to delete the last character of the StringBuilder object. The last character index can be found using string.length() – 1.

public class Main {
   public static void main(String[] args) {
      StringBuilder string = new StringBuilder("The pen is mightier than the sword");
      System.out.println("StringBuilder = " + string.toString());
      StringBuilder afterRemoval = string.deleteCharAt(string.length() - 1);
      System.out.println("After removal = " + afterRemoval);
   }
}

Output:

StringBuilder = The pen is mightier than the sword
After removal = The pen is mightier than the swor

Delete A Specific Character StringBuilder Java Example

We can also delete a specific character from StringBuilder in Java at a given index using the deleteCharAt() method.

public class Main {
   public static void main(String[] args) {
      StringBuilder string = new StringBuilder("The pen is mightier than the sword");
      System.out.println("StringBuilder = " + string.toString());
      StringBuilder afterRemoval = string.deleteCharAt(5);
      System.out.println("After removal = " + afterRemoval);
   }
}

Output:-

StringBuilder = The pen is mightier than the sword
After removal = The pn is mightier than the sword

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *