Java-Insert

Insert

The insert( ) method is a companion method of the append( ) method. While the append( ) method enables us to add a new string only at the end of an existing string, the insert( ) method helps us to add a new string from any specified position in the existing string. For example, in the following program, the new string “CEBO” will be inserted at the index position ‘2’ in the string “FAOK”. As a result, the string ‘str’ will now become “FACEBOOK”. The output statement will display the string “FACEBOOK”.

Program

final2

Program Source

public class Javaapp {
    
    public static void main(String[] args) {
        
        StringBuffer str = new StringBuffer();
        str.append("FAOK");
        str.insert(2,"CEBO");
        System.out.println("str content : "+str);
    }
}

Leave a Comment