[Common Java class libraries] _ stringbuffer
Stringbuffer instance 1:
Public class stringbufferdemo01 {public static void main (string ARGs []) {stringbuffer Buf = new stringbuffer (); // declare the stringbuffer object Buf. append ("hello"); // Add content Buf to stringbuffer. append ("world "). append ("!!! "); // You can call the append () method Buf continuously. append ("\ n"); // Add an escape character Buf. append ("number = "). append (1 ). append ("\ n"); // Add a number Buf. append ("character = "). append ('C '). append ("\ n"); // Add the character Buf. append ("Boolean = "). append (true); // Add a Boolean value system. out. println (BUF); // directly outputs the object and calls tostring ()}};
Stringbuffer instance 2: (receive reference transfer information)
Public class stringbufferdemo02 {public static void main (string ARGs []) {stringbuffer Buf = new stringbuffer (); // declare the stringbuffer object Buf. append ("hello"); fun (BUF); // pass the stringbuffer content system. out. println (BUF); // print content} public static void fun (stringbuffer s) {// receives stringbuffer reference S. append ("mldn "). append ("lixinghua"); // modify the content of stringbuffer }};
Stringbuffer Example 3: (insert method)
Public class stringbufferdemo03 {public static void main (string ARGs []) {stringbuffer Buf = new stringbuffer (); // declare the stringbuffer object Buf. append ("world !! "); // Add content Buf. insert (0, "hello"); // Add the content system before the first content. out. println (BUF); Buf. insert (BUF. length (), "mldn ~ "); // Add the content system. Out. println (BUF) ;}} at the end );}};
Stringbuffer Example 4: (Reverse Method -- string inversion)
Public class stringbufferdemo04 {public static void main (string ARGs []) {stringbuffer Buf = new stringbuffer (); // declare the stringbuffer object Buf. append ("world !! "); // Add content Buf. insert (0, "hello"); // Add the content string STR = Buf before the first content. reverse (). tostring (); // reverse the content and change it to the string type system. out. println (STR); // output content }};
Stringbuffer instance 5: (replace method)
Public class stringbufferdemo05 {public static void main (string ARGs []) {stringbuffer Buf = new stringbuffer (); // declare the stringbuffer object Buf. append ("hello "). append ("world !! "); // Add content Buf to stringbuffer. replace (6, 11, "lixinghua"); // Replace the world content with system. out. println ("result after content replacement:" + BUF); // output content }};
Stringbuffer instance 6: (substring)
Public class stringbufferdemo05 {public static void main (string ARGs []) {stringbuffer Buf = new stringbuffer (); // declare the stringbuffer object Buf. append ("hello "). append ("world !! "); // Add content Buf to stringbuffer. replace (6, 11, "lixinghua"); // Replace the world content with system. out. println ("result after content replacement:" + BUF); // output content }};
Stringbuffer instance 7: (delete operation)
Public class stringbufferdemo07 {public static void main (string ARGs []) {stringbuffer Buf = new stringbuffer (); // declare the stringbuffer object Buf. append ("hello "). append ("world !! "); // Add content Buf to stringbuffer. replace (6, 11, "lixinghua"); // Replace the world content with string STR = Buf. delete (6, 15 ). tostring (); // Delete the content in the specified range system. out. println ("result after deletion:" + Str); // output content }};
Stringbuffer instance 8: (indexof Operation)
Public class stringbufferdemo08 {public static void main (string ARGs []) {stringbuffer Buf = new stringbuffer (); // declare the stringbuffer object Buf. append ("hello "). append ("world !! "); // Add content to stringbuffer if (BUF. indexof ("hello") =-1) {system. out. println ("no specified content found");} else {// if not 01, the system is found. out. println ("the specified content can be found ");}}};
Stringbuffer instance 9: (Performance Comparison Between stringbuffer and string)
Public class stringbufferdemo09 {public static void main (string ARGs []) {string str1 = "lixinghua"; for (INT I = 0; I <100; I ++) {str1 + = I; // continuously modify the memory reference of string, with 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); // The stringbuffer can be modified, with high performance} system. out. println (BUF );}};