1. Add function of StringBuffer:
Public StringBuffer Append (String str): You can add any type of data to the string buffer and return the string buffer itself.
Public stringbuffer Insert (int offset, string str): Inserts any type of data into the string buffer at the specified location and returns the string buffer itself .
Note: Both methods return the string buffer object itself, so that memory can be saved.
2. Case Demo:
1 Packagecn.itcast_02;2 3 /*4 * Add function of StringBuffer:5 * Public stringbuffer Append (String str): You can add any type of data to the string buffer and return the string buffer itself6 * 7 * Public stringbuffer Insert (int offset,string str): Inserts any type of data into the string buffer at the specified location and returns the string buffer itself8 */9 Public classStringbufferdemo {Ten Public Static voidMain (string[] args) { One //creating a String buffer object AStringBuffer SB =NewStringBuffer (); - - //Public stringbuffer Append (String str) the //StringBuffer SB2 = sb.append ("Hello"); - //System.out.println ("SB:" + SB); - //System.out.println ("SB2:" + SB2); - //System.out.println (sb = = SB2);//true here is true because both SB and SB2 are pointing to the same StringBuffer object, returning the string buffer itself + - //Add data step by step + //sb.append ("Hello"); A //Sb.append (true); at //sb.append (n); - //sb.append (34.56); - - //Chained Programming -Sb.append ("Hello"). Append (true). Append. Append (34.56); -System.out.println ("SB:" +SB); in - //Public stringbuffer Insert (int offset,string to //str): Inserts any type of data into the string buffer at the specified location and returns the string buffer itself +Sb.insert (5, "World"); -System.out.println ("SB:" +SB); the } *}
The results of the operation are as follows:
Java basic knowledge to enhance the stringbuffer of 39:stringbuffer class