Asp.net simple application StringBuilder class (1/2)

Source: Internet
Author: User

Stringbuilder attributes and methods:
--------------------------------------------------------------------------------
 
/* Attribute */
Capacity; // capacity; it can be read and written, or set during build, but ensurecapacity () is more secure when rewriting
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. It can only be set at build time. If it exceeds the upper limit, an exception occurs.

/* Method */
Append (); // append; to accept different types of data, it has many
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 (); // sets 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; // namespace to which stringbuilder belongs

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;
}

Homepage 1 2 Last page
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.