Wednesday, 9 July 2014

setCharAt (int index, char ch)

public synchronized void setCharAt(int index, char ch)
The character at the specified index is set to ch.

public class StringBufferSetCharAt {
    public static void main(String args[]){
        StringBuffer st = new StringBuffer("abababab");
        
        System.out.println("Before :" + st);
        
        for(int i=0; i < st.length(); i++){
            if(i%2 == 0)
                st.setCharAt(i, 'b');
            else
                st.setCharAt(i, 'a');
        }
        
         System.out.println("After :" + st);
    }
}

Output
Before :abababab
After :babababa


1. Throws IndexOutOfBoundsException, if index is negative or greater than or equal to length().



 
Prevoius                                                 Next                                                 Home

No comments:

Post a Comment