The example below show you to remove some elements of the StringBuffer
. We can use the delete(int start, int end)
method call to remove some characters from the specified start
index to end end
index. We can also remove a character at the specified index using the deleteCharAt(int index)
method call.
package org.kodejava.lang;
public class StringBufferDelete {
public static void main(String[] args) {
String text = "Learn Java by Examples";
// Creates a new instance of StringBuffer and initialize
// it with some text.
StringBuffer buffer = new StringBuffer(text);
System.out.println("Original text = " + buffer);
// We'll remove a sub string from this StringBuffer starting
// from the first character to the 10th character.
buffer.delete(0, 10);
System.out.println("After deletion = " + buffer);
// Removes a char at a specified index from the StringBuffer.
// In the example below we remove the last character.
buffer.deleteCharAt(buffer.length() - 1);
System.out.println("Final result = " + buffer);
}
}
Output of the program is:
Original text = Learn Java by Examples
After deletion = by Examples
Final result = by Example
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024