public class StringBufferDemo01 { public static void main (String args []) { StringBuffer buf = new new StringBuffer (); // declare StringBuffer object buf.append ( "the Hello"); // add content to the StringBuffer buf.append ( "World ") .append (" !!! "); // continuously calling append () method buf.append (" \ the n-"); // add an escape character buf.append (" digital = ") .append ( . 1) .append ( "\ n-"); // add a digital buf.append ( "character =") .append ( 'C' ) append ( "\ n").;// add character buf.append ( "boolean =") .append ( to true ); // add Boolean System.out.println (buf); // direct output object, call toString () } };
public class StringBufferDemo02 { public static void main (String args []) { StringBuffer buf = new new StringBuffer (); // declare StringBuffer object buf.append ( "the Hello" ); Fun (buf); // pass StringBuffer content System.out .println (buf); // print } public static void Fun (StringBuffer S) { // receiving StringBuffer reference s.append ( "MLDN") .append ( "LiXingHua"); // content of StringBuffer } };
public class StringBufferDemo03 { public static void main (String args []) { StringBuffer buf = new new StringBuffer (); // declare StringBuffer object buf.append ( "World !!"); // add content buf.insert (0, " the Hello "); // add content before the first content System.out.println (buf); buf.insert (to buf.length (), " MLDN ~ "); // at the end add content System.out.println (buf); } };
public class StringBufferDemo04 { public static void main (String args []) { StringBuffer buf = new new StringBuffer (); // declare StringBuffer object buf.append ( "World !!"); // add content buf.insert (0, " hello "); // added before the first contents String = buf.reverse STR () toString ();. // content after inversion becomes a String System.out.println (STR); // the content output } };
public class StringBufferDemo05 { public static void main (String args []) { StringBuffer buf = new new StringBuffer (); // declare StringBuffer object buf.append ( "the Hello") .append ( "World !!"); // to StringBuffer add content buf.replace (6,11, "LiXingHua"); // the world's replaces System.out.println ( "replaces following results:" + buf); // outputting content } };
public class StringBufferDemo06 { public static void main (String args []) { StringBuffer buf = new new StringBuffer (); // declare StringBuffer object buf.append ( "the Hello") .append ( "World !!"); // to StringBuffer add content buf.replace (6,11, "LiXingHua"); // the world replaces the String STR = buf.substring (6,15); // taken content specified range System.out.println ( "replaces after results: "+ STR); // outputting content } };
public class StringBufferDemo07 { public static void main (String args []) { StringBuffer buf = new new StringBuffer (); // declare StringBuffer object buf.append ( "the Hello") .append ( "World !!"); // to StringBuffer add content buf.replace (6, 11, "LiXingHua"); // the world's replaces String str = buf.delete (6, 15) .toString (); // delete the content in the specified range System.out. println ( "result after:" + STR); // outputting content } };
public class StringBufferDemo08 { public static void main (String args []) { StringBuffer buf = new new StringBuffer (); // declare StringBuffer object buf.append ( "the Hello") .append ( "World !!"); // to StringBuffer add content IF (buf.indexOf ( "the Hello") == -. 1 ) { System.out.println ( "not find the specified content" ); } the else { // not find the content 01 showing System.out. the println ( "You can find specific content" ); } } };
public class StringBufferDemo09 { public static void main (String args []) { String str1 = "LiXingHua" ; for ( int I = 0; I <100; I ++ ) { str1 + = I; // continue to modify the memory references a String, low performance } System.out.println (str1); } };
public class StringBufferDemo10{ public static void main(String args[]){ StringBuffer buf = new StringBuffer() ; buf.append("LiXingHua") ; for(int i=0;i<100;i++){ buf.append(i); // StringBuffer可以修改,性能高 } System.out.println(buf) ; } };