setCharAt(int index, char ch)
You can set the value of a character within a StringBuffer using setCharAt( ).
void setCharAt(int index, char ch)
- The character at the specified index is set to ch.
The following example demonstrates charAt( ) and setCharAt( ):
public class Main {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("java2s.com");
System.out.println("buffer before = " + sb);
System.out.println("charAt(1) before = " + sb.charAt(1));
sb.setCharAt(1, 'i');
sb.setLength(2);
System.out.println("buffer after = " + sb);
System.out.println("charAt(1) after = " + sb.charAt(1));
}
}
The output:
buffer before = java2s.com
charAt(1) before = a
buffer after = ji
charAt(1) after = i
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