The reason for the existence of the stringbuilder class is that it is faster than the string class when adding, deleting, modifying, and modifying strings. The faster reason is that it does not need to be released and rebuilt repeatedly like a string.
However, it will re-allocate the memory as needed. If the required memory is premade, it will be faster.
Stringbuilder attributes and methods:
/* Attribute */ Capacity; // Capacity; it can be read and written, or set during build, but ensurecapacity () is more secure during rewrite Length; // Length; it can be read and written. When writing 0, it is the same as clear (); length <= capacity <= maxcapacity Maxcapacity; // Maximum capacity; read-only, which can only be set at build time. If the maximum capacity is exceeded, an exception occurs. /* Method */ Append (); // Append; to accept different types of data, it has many reloads Appendformat ();// Append by format Appendline (); // Append a line break Clear (); // Cancel; length = 0; but capacity and maxcapacity remain unchanged Copyto (); // Copy the specified part to Char [] Ensurecapacity (); // Set capacity Insert (); // Insert Remove (); // Remove Replace (); // Replace Tostring (); // Output text, which can be captured at the same time
Six Types of constructor overloading:
Using System. text; // The namespace of stringbuilder Protected Void Button#click ( Object Sender, Eventargs E ){ String STR = "" ; /* If no parameter exists, capacity defaults to 16 */ Stringbuilder SB1 = New Stringbuilder (); STR + = sb1.capacity. tostring ("Capacity: # \ n" ); // Capacity: 16 /* Specify the capacity size during build */ Stringbuilder Sb2 = New Stringbuilder (11); STR + = sb2.capacity. tostring ( "Capacity: # \ n" ); // Capacity: 11 /* When constructing a string, if the length of the string is greater than 16, its capacity is the same as the length of the string */ Stringbuilder Sb3 = New Stringbuilder ( "Abcdefghijklmnopqrstuvwxyz" ); STR + = sb3.capacity. tostring ( "Capacity: # \ n" ); // Capacity: 26 /* Specify capacity and maxcapacity */ Stringbuilder Sb4 = New Stringbuilder (4, 10); STR + = sb4.capacity. tostring ( "Capacity: # \ t" ); // Capacity: 4 Sb4.append ( "1234567890" ); STR + = sb4.capacity. tostring ( "Capacity: # \ n" ); // Capacity: 10 Try {Sb4.append ( "ABC" );} Catch (Exception ERR) {STR + = err. Message + "\ N" ;} // The capacity is smaller than the current size... /* Construct with strings and specify capacity */ Stringbuilder Sb5 = New Stringbuilder ("1234567890" , 32); STR + = String . Format ( "Capacity: {0} \ tlength: {1} \ n" , Sb5.capacity, sb5.length ); // Capacity: 32 length: 10 /* Extract the build from the string and specify capacity */ Stringbuilder Sb6 = New Stringbuilder ( "Abcdefg" , 1, 3, 12); STR + = String . Format ( "\" {0 }\"\ Tcapacity: {1} \ tlength: {2 }" , Sb6, sb6.capacity, sb6.length ); // "BCD" Capacity: 12 length: 3 Textbox1.text = STR ;}
Exercise:
// Tostring () Protected Void Button#click ( Object Sender, Eventargs E ){ Stringbuilder SB = New Stringbuilder ( "1234567890" ); String S1 = sb. tostring (); // 1234567890 String S2 = sb. tostring (2, 3 ); // 345 Textbox1.text = S1 + "\ N" + S2 ;} // Append (), appendformat (), appendline () Protected Void Button2_click ( Object Sender, Eventargs E ){ Stringbuilder SB =New Stringbuilder (256); sb. append ( "Abcdefg" ); Sb. appendline (); sb. append (3.1415926); sb. appendline (); sb. appendformat ( "{0 :#.##}" , 3.1415926); textbox1.text = sb. tostring (); /* Abcdefg3.14159263.14 */ } // Insert (), remove (), replace () Protected Void Button3_click ( Object Sender, Eventargs E ){Stringbuilder SB = New Stringbuilder ( ".........." , 256 ); String S1 = sb. insert (2, "ABC" ). Tostring (); //... ABC ........ String S2 = sb. insert (2, 3.14). tostring (); //. 3.14abc ........ String S3 = sb. Remove (6, 3). tostring (); //... 3. 14 ........ String S4 = sb. Replace ( '.' , '*' ). Tostring (); // ** 3*14 ******** Textbox1.text = S1 + "\ N" + S2 + "\ N" + S3 + "\ N" + S4 ;} // Copyto Protected Void Button4_click ( Object Sender, Eventargs E ){String STR = ".........." ; Char [] Carr = Str. tochararray (); Stringbuilder SB = New Stringbuilder ( "1234567890" ); Sb. copyto (2, Carr, 0, 3); textbox1.text = New String (CARR ); // 345 ....... }