Tuesday 8 July 2014

insert (int offset, Object obj)

public synchronized StringBuffer insert(int offset, Object obj)
Inserts the string representation of the object into this sequence at the indicated offset.

class Student{
    int id;
    String name;
    
    Student(int id, String name){
        this.id = id;
        this.name = name;
    }
    
    @Override
    public String toString(){
        return id + "." + name;
    }
}

public class StringBufferInsertObject {
    public static void main(String args[]){
       Student stud = new Student(1, "Krishna");
       StringBuffer str = new StringBuffer("Value of stud is ");
       str.insert(str.length(), stud);
       System.out.println(str);
    }
}

Output
Value of stud is 1.Krishna

1. Throws StringIndexOutOfBoundsException, if the offset is invalid.



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment