Java StringBuffer insert Example
StringBuffer class insert method example. This example shows you how to use insert method.
StringBuffer class insert method example. This example shows you how to use insert method.Inserts the string representation of the different type of argument into the StringBuffer.
Here is the code
/**
* @(#) Insert.java
* A class representing insert method
* @Version 01-May-2008
* @author Rose India Team
*/
public class Insert {
public static void main(String[] args){
StringBuffer buf = new StringBuffer("Is it ?");
// Insert true at offset 6:
buf = buf.insert(6,true);
// Insert # at the offset 11:
buf = buf.insert(11,'#');
System.out.println(buf);
// Construct a character array:
char[] str = {' ','H','i',' '};
// Insert str at the offset 11:
buf = buf.insert(11,str);
System.out.println(buf);
//inserts the subarray of the str array argument into this string buffer.
buf = buf.insert(3,str,1,3);
System.out.println(buf);
// Insert double value at the offset 14:
double l=10.101;
buf = buf.insert(14,l);
System.out.println(buf);
// Insert float value at the offset 21:
Float f = new Float(1.1);
buf = buf.insert(21,f);
System.out.println(buf);
// Insert int value at the offset 14:
int i=9;
buf = buf.insert(14,i);
System.out.println(buf);
// Insert long value at the offset 14:
long lo=1000;
buf = buf.insert(14,lo );
System.out.println(buf);
// Insert a object at the offset 0:
buf = buf.insert(0,buf );
System.out.println(buf);
// Insert a string at the offset 14:
buf = buf.insert(14,"new" );
System.out.println(buf);
}
} |
null
|