replace():replace a StringBuffer
replace( ) can replace one set of characters with another set inside a StringBuffer.
StringBuffer replace(int start, int end, String str)
- Replaces the characters in a substring of this sequence with characters in the specified String.
The substring being replaced is specified by the indexes startIndex and endIndex. substring at startIndex through endIndex-1 is replaced. The replacement string is passed in str. The resulting StringBuffer object is returned.
The following program demonstrates replace( ):
public class Main {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("This is a test.");
sb.replace(5, 7, "was");
System.out.println("After replace: " + sb);
}
}
Here is the output:
After replace: This was a test.
Home
Java Book
Essential Classes
Java Book
Essential Classes
StringBuffer:
- Create StringBuffer object
- append
- StringBuffer capacity()
- charAt(int index): get the char at specified index
- delete(int start, int end) and deleteCharAt(int index)
- ensureCapacity( )
- getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
- indexOf(String str)
- lastIndexOf(String str)
- StringBuffer length()
- Insert(): add data in the middle of a StringBuffer
- replace():replace a StringBuffer
- StringBuffer reverse()
- setCharAt(int index, char ch)
- setLength
- substring
- toString():Convert StringBuffer to String