The java StringBuilder deleteCharAt() method is used to remove the character at the specified position in this sequence. This sequence is shortened by one character.
In Java, the indexes are refers to the character positions of the current sequence. Basically, the charAt() method is used to retrieve a character at the specified index.The deleteCharAt() method accepts a parameter as an integer that holds the value of the character index. It throws an exception if the index value is negative or the value is greater than the given sequence length.
SyntaxFollowing is the syntax of the Java StringBuilder deleteCharAt() method −
public StringBuilder deleteCharAt(int index)Parameters
index − This is the index of char to remove.
This method returns the substring of the given sequence.
Example: Deleting a Substring from a StringBuilderIf the index value is non-negative and less than the sequence length, this method returns the new substring of the given sequence.
In the following example,we are creating an object of the StringBuilder class with the value Java lang package. Using the delete() method, we are trying to delete a character at the specified index 3.
public class StringBuilderDemo { public static void main(String[] args) { //create an object of the StringBuilder StringBuilder sb = new StringBuilder("Java lang package"); System.out.println("Before deletion the string is: " + sb); //initialize the index value int index = 3; System.out.println("The given index value is: " + index); //using the deleteCharAt() method System.out.println("After deletion the character at the specified index " + index + " index is: " + sb.deleteCharAt(index)); } }Output
On executing the above program, it will produce the following result −
Before deletion the string is: Java lang package The given index value is: 3 After deletion the character at the specified index 3 index is: Jav lang packageExample: Facing StringIndexOutOfBoundsException while Deleting a Substring from a StringBuilder
If the index value if greater than the given sequence length, this method throws an StringIndexOutOfBoundsException.
In this program,then we are instantiating the StringBuilder class with the value JavaProgramming. Using the deleteCharAt() method, we are trying to delete a character at the specified index 30, whose value is greater than the given sequence.
public class Delete { public static void main(String[] args) { try { //create an object of the StringBuilder StringBuilder sb = new StringBuilder("JavaProgramming"); System.out.println("Before deletion the string is: " + sb); //initialize the index value int index = 30;// value is greater than the sequence length System.out.println("The given index value is: " + index); //using the deleteCharAt() method System.out.println("After deletion rhe remaing string is: " + sb.deleteCharAt(index)); } catch(IndexOutOfBoundsException e) { e.printStackTrace(); System.out.println("Exception: " + e); } } }Output
Following is the output of the above program −
Before deletion the string is: JavaProgramming The given index value is: 30 java.lang.StringIndexOutOfBoundsException: Index 30 out of bounds for length 15 at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55) at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210) at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302) at java.base/java.lang.String.checkIndex(String.java:4557) at java.base/java.lang.AbstractStringBuilder.deleteCharAt(AbstractStringBuilder.java:957) at java.base/java.lang.StringBuilder.deleteCharAt(StringBuilder.java:486) at Delete.main(Delete.java:13) Exception: java.lang.StringIndexOutOfBoundsException: Index 30 out of bounds for length 15Example: Facing StringIndexOutOfBoundsException while Deleting a Substring from a StringBuilder
If the index value contains a negative value, it throws a StringIndexOutOfBoundsException, when we invoke the method on it.
In the following example, we create a StringBuilder with the value TutorialsPoint. Using the deleterCharAt() method, we are trying to delete the character at the specified index -1.
public class Demo { public static void main(String[] args) { try { //create an object of the StringBuilder StringBuilder sb = new StringBuilder("TutorialsPoint"); System.out.println("Before deletion the string is: " + sb); //initialize the index value int index = -1;// holds the negative value System.out.println("The given index value is: " + index); //using the deleteCharAt() method System.out.println("After deletion rhe remaing string is: " + sb.deleteCharAt(index)); } catch(IndexOutOfBoundsException e) { e.printStackTrace(); System.out.println("Exception: " + e); } } }Output
The above program, produces the following results −
Before deletion the string is: TutorialsPoint The given index value is: -1 java.lang.StringIndexOutOfBoundsException: Index -1 out of bounds for length 14 at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55) at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210) at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302) at java.base/java.lang.String.checkIndex(String.java:4557) at java.base/java.lang.AbstractStringBuilder.deleteCharAt(AbstractStringBuilder.java:957) at java.base/java.lang.StringBuilder.deleteCharAt(StringBuilder.java:486) at Demo.main(Demo.java:13) Exception: java.lang.StringIndexOutOfBoundsException: Index -1 out of bounds for length 14
java_lang_stringbuilder.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4