/*
StringBuffer is a string buffer
is a container. Features: (data type is not OK, the number is not OK to use)
1, and the length is changeable.
2. You can manipulate multiple data types directly.
3. Eventually, the ToString method becomes the string.
Curd
C Create U update R read D Delete
1, storage.
StringBuffer append (): Adds the specified data as a parameter to the end of the existing data.
StringBuffer Insert (index, data); You can insert data into the specified index position
2. Delete
StringBuffer Delete (start,end): Deletes buffer data, contains start, does not contain end
StringBuffer Deletecharat (index); Deletes the character at the specified position.
3. Get
Char charAt (int index);
int indexOf (String str);
LastIndexOf (String str);
int length ();
String substring (int start,int end); Returns a string
4. Modification.
StringBuffer replace (start,end,string);
void Setcharat (int index,char ch)
5, reverse.
StringBuffer reverse ();
6. Stores the specified data in the buffer in the specified character array.
void GetChars (int srcbegin, int srcend, char[] DST, int dstbegin)
Purpose to start the bit
After the jdk1.5 version appears StringBuffer
StringBuffer is thread synchronization
StringBuilder is a thread that does not synchronize to improve the efficiency of the method just as there is no lock
Later development established, using StringBuilder
Three factors for upgrading Java:
1, improve efficiency;
2, simplified writing;
3, improve security.
*/
Public classStringbufferdemo { Public Static voidsop (Object obj) {System.out.println (obj); } Public Static voidMain (string[] args) {//method_update ();StringBuilder SB =NewStringBuilder ("ABCdef"); Char[] CHS =New Char[6]; Sb.getchars (1, 4, CHS, 1); for(intx=0;x<chs.length;x++) {SOP ("chs[" +x+ "]=" +chs[x]+ ";"); } } Public Static voidmethod_update () {StringBuffer sb=NewStringBuffer ("ABCDE"); //sb.replace (1, 4, "Java");Sb.setcharat (2, ' K '); SOP (Sb.tostring ()); } Public Static voidMethod_del () {StringBuffer sb=NewStringBuffer ("ABCDE"); //Sb.delete (1, 3); //empties the buffer!!! //sb.delete (0, Sb.length ());Sb.delete (2, 3); //Sb.deletecharat (2);SOP (Sb.tostring ()); } Public Static voidMethod_add () {StringBuffer sb=NewStringBuffer (); //method call chain, return or object so that the method can be calledSb.append ("abc"). Append (true). Append (34);//StringBuffer sb1 = Sb.append (a);// //SOP ("SB==SB1" +sb==sb1);StringBuffer insert = Sb.insert (1, "QQ"); SOP (Sb.tostring ());//Abctrue34//SOP (Sb1.tostring ()); }}
StringBuffer is a string buffer