91
Java StringBuffer deleteCharAt(int index) method
The deleteCharAt(int index) method of Java StringBuffer class is used to delete the character at a specified position of this sequence. After deleting a character, the current sequence shortened by one character.
Syntax:
Parameter:
DataType | Parameter | Description |
---|---|---|
int | index | The index is location of character which is to delete. |
Returns:
This deleteCharAt(int index) method returns this object.
Exception:
StringIndexOutOfBoundsException – if the index is negative or greater than or equal to length() of string buffer.
Compatibility Version:
Java 1.2 and above
Example 1
Output:
string1: TutorAspire After deleting: javapoint string2: hello java After deleting: hellojava
Example 2
This deleteCharAt(int) method throw the exception while providing the negative index value.
Output:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.AbstractStringBuilder.deleteCharAt(Unknown Source) at java.lang.StringBuffer.deleteCharAt(Unknown Source) at StringBufferDeleteCharAtExample2.main(StringBufferDeleteCharAtExample2.java:6) string1 = TutorAspire
Example 3
Deleting the character at the index value of Character.charCount(thisSequence.codePointAt(index)) method.
Output:
string: TutorAspire codepoint at 0: 106 total character at codepoint 106 : 1 after deleting at index 1: jvatpoint
Example 4
In this example, we are taking the input string and index of character from a user.
Output:
enter your string value: hello string enter index: 3 after deleting resultant string: helo string
Next TopicStringBuffer class