Learning ASP. NET "string" (5): stringbuilder class

Source: Internet
Author: User
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 construction, but ensurecapacity () is used for rewriting to ensure length; // length; it can be read and written, when writing 0, it is the same as clear (); Length
Six Types of constructor overloading:
Using system. text; // The namespace protected void button#click (Object sender, eventargs e) {string STR = "";/* if no parameter exists, capacity default 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/* If the string length 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/* build from string truncation, 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;} // copytoprotected 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 .......}
Related Article

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.