Comparison of stringbuffer and stringbuilder usage

Source: Internet
Author: User

Stringbuffer is a string buffer and a container.


Features:
1. The length is changeable.
2. Multiple data types can be operated in bytes.

3. The tostring method is used to convert the string.


C create U update R read d Delete

1. Storage.
Stringbuffer append (): add the specified data as a parameter to the end of the existing data.
Stringbuffer insert (index, data): You can insert data to the specified index position.

2. Delete.
Stringbuffer Delete (START, end): delete data in the buffer, including start, not end.
Stringbuffer deletecharat (INDEX): delete a character at a specified position.

3. obtain.
Char charat (INT index)
Int indexof (string Str)
Int lastindexof (string Str)
Int length ()
String substring (INT start, int end)
 
4. modify.
Stringbuffer Replace (START, end, string );
Void setcharat (INT index, char ch );

5. Reverse.
Stringbuffer reverse ();
 
6,
Store the specified data in the buffer to the specified character array.
Void getchars (INT srcbegin, int srcend, char [] DST, int dstbegin)
Example:
Stringbuffer buffer = new stringbuffer ("abcdef ");
Char [] CHS = new char [5];
Buffer. getchars (1, 4, CHS, 0 );
For (INT I = 0; I <CHS. length; I ++ ){
System. Out. println ("CHS [" + I + "]" + "=" + CHS [I] + ";");
}
Output: CHS [0] = B;
CHS [1] = C;
CHS [2] = D;
CHS [3] =;
CHS [4] =;
Stringbuilder is displayed after jdk1.5.

Stringbuffer is used for thread synchronization.
Stringbuilder does not synchronize threads.


We recommend that you use stringbuilder for future development.

Three factors for upgrading:
1. improve efficiency.
2. Simplified writing.

3. improve security.

Class stringbufferdemo {public static void main (string [] ARGs) {// method_update (); stringbuilder sb = new stringbuilder ("abcdef "); char [] CHS = new char [6]; sb. getchars (, CHS, 1); // convert for (INT x = 0; x <chs. length; X ++) {Sop ("CHS [" + x + "] =" + CHS [x] + ";") ;}draw (3,6 ); draw (8, 9); // stringbuilder SB1 = new stringbuilder (); // sb1.append (new demo ()). append (new demo (); // Sop ("SB1 =" + SB1);} public static void method_update () {Stringbuffer sb = new stringbuffer ("ABCDE"); // sb. replace (1, 4, "Java"); sb. setcharat (2, 'k'); Sop (sb. tostring ();} public static void method_del () {stringbuffer sb = new stringbuffer ("ABCDE"); // sb. delete (1, 3); // clear the buffer. // Sb. delete (0, sb. length (); // sb. delete (2, 3); sb. deletecharat (2); Sop (sb. tostring ();} public static void method_add () {stringbuffer sb = new stringbuffer (); // sb. append ("ABC "). append (true ). append (34); // stringbuffer SB1 = sb. append (34); // Sop ("SB = SB1:" + (SB = SB1); sb. insert (1, "QQ"); Sop (sb. tostring (); // abctrue34 // Sop (sb1.tostring ();} public static void Sop (string Str) {system. out. println (STR);} public static void draw (INT row, int col) {stringbuilder sb = new stringbuilder (); For (INT x = 0; x <row; X ++) {for (INT y = 0; y <Col; y ++) {sb. append ("*");} sb. append ("\ r \ n");} Sop (sb. tostring ());}}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.