Java StringBuffer deleteCharAt(int index) methodThe 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:
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 1Test it NowOutput: string1: javatpoint After deleting: javapoint string2: hello java After deleting: hellojava Example 2This deleteCharAt(int) method throw the exception while providing the negative index value. Test it NowOutput: 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 = javatpoint Example 3Deleting the character at the index value of Character.charCount(thisSequence.codePointAt(index)) method. Test it NowOutput: string: javatpoint codepoint at 0: 106 total character at codepoint 106 : 1 after deleting at index 1: jvatpoint Example 4In 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
|