In this article, we will discuss StringBuffer’s deleteCharAt() method which deletes a char-value at specified index-position
1. StringBuffer’s deleteCharAt() method:
- This method deletes single character from the invoking StringBuffer object at the specified index-position
1.1 Method Signature:
public StringBuffer deleteCharAt(int index);
1.2 Returns:
- Returns StringBuffer object after deleting single character at the specified index-position
1.3 Throws:
- StringIndexOutOfBoundsException, if index-value passed falls out of range i.e.;
- if specified index value negative (<0)
- if specified index value is greater than length()
2. Examples on deleteCharAt() method:
- To delete single char-value from invoking StringBuffer object at the specified index-position
StringBufferDeleteCharAtMethod.java
package in.bench.resources.stringbuffer.methods;
public class StringBufferDeleteCharAtMethod {
public static void main(String[] args) {
// StringBuffer - 1
StringBuffer sb1 = new StringBuffer("BenchResources.Net");
// removes single character at specified index-position
sb1.deleteCharAt(14);
// print to console
System.out.println("1. after deleting single char"
+ " from StringBuffer at 14th index : " + sb1);
// StringBuffer - 2
StringBuffer sb2= new StringBuffer("OnlineTutorials.com");
// removes single character at specified index-position
sb2.deleteCharAt(5);
// print to console
System.out.println("2. after deleting single char"
+ " from StringBuffer at 5th index : " + sb2);
}
}
Output:
1. after deleting single char from StringBuffer at 14th index :
BenchResourcesNet
2. after deleting single char from StringBuffer at 5th index :
OnlinTutorials.com
Related Articles:
- Java – StringBuffer class
- Java – StringBuffer append() method (13)
- Java – StringBuffer capacity() method
- Java – StringBuffer charAt(int index) method
- Java – StringBuffer delete(int start, int end) method
- Java – StringBuffer deleteCharAt(int index) method
- Java – StringBuffer ensureCapacity(int minimumCapacity) method
- Java – StringBuffer indexOf() method (2)
- Java – StringBuffer insert() method (12)
- Java – StringBuffer lastIndexOf() method (2)
- Java – StringBuffer length() method
- Java – StringBuffer replace(int start, int end, String str) method
- Java – StringBuffer reverse() method
- Java – StringBuffer substring(int start, int end) method
References:
- https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
Happy Coding !!
Happy Learning !!