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