The difference between StringBuilder and string

Source: Internet
Author: User

Ext.: http://blog.163.com/zhaoyanping_1125/blog/static/201329153201204111726152/

To summarize, in short, the difference between StringBuilder and string is:

StringBuilder is a long-growing one.

string is fixed-length.

The following describes the use of string and StringBuilder in detail:

-------------------------------------------------------------------------------------------------------

Let's look at an example first.
string S1 = "Orange";
string s2 = "Red";

S1 + = s2;
System.Console.WriteLine (S1); Outputs "orangered"

S1 = S1. Substring (2, 5);
System.Console.WriteLine (S1); Outputs "Anger"

We all know that the string object is "immutable",
A method that operates on a string actually returns a new string object.
In the previous example, when the contents of S1 and S2 were concatenated to form a string, the two strings containing "orange" and "red" remained unchanged. The + = operator creates a new string containing the combined content.

The result is:

S1 now refers to a completely different string. A string containing only "orange" still exists, but will no longer be referenced after the connection is S1.
When a large number of strings are added, there will be a lot of things like S1 that are not referenced, resulting in a great waste of resources.

-------------------------------------------------------------------------------------------------------

We are looking at how StringBuilder is dealing with such problems.

System.Text.StringBuilder sb = new System.Text.StringBuilder ();
Sb. Append ("one");
Sb. Append ("both");
Sb. Append ("three");
String str = sb. ToString ();

I. Definition of StringBuilder

The String object is immutable. Each time you use one of the methods in the System.String class, you create a new string object in memory, which requires a new space to be allocated for the new object. The overhead associated with creating a new string object can be very expensive in situations where you need to perform repeated modifications to the string. If you want to modify a string without creating a new object, you can use the System.Text.StringBuilder class.
For example, using the StringBuilder class can improve performance when you concatenate many strings together in a loop.
By initializing the variable with an overloaded constructor method, you can create a new instance of the StringBuilder class, as in the following example:


For example: StringBuilder mystringbuilder = new StringBuilder ("Hello world!");

Second, set the capacity and length

1.

Although the StringBuilder object is a dynamic object, it allows you to expand the number of characters in the string it encapsulates, but you can specify a value for the maximum number of characters it can hold. This value is called the capacity of the object and should not be confused with the length of the string that the current StringBuilder object holds. For example, you can create a new instance of the StringBuilder class with the string "Hello" (length 5), and you can specify that the object has a maximum capacity of 25. When StringBuilder is modified, it does not redistribute space for itself until the capacity is reached. When the capacity is reached, new space is automatically allocated and the capacity doubles. You can use one of the overloaded constructors to specify the capacity of the StringBuilder class. The following code example specifies that the Mystringbuilder object can be expanded to a maximum of 25 blanks.

For example: StringBuilder mystringbuilder = new StringBuilder ("Hello world!", 25);

2. Alternatively, you can use the read/write capacity property to set the maximum length of the object. The following code example uses the capacity property to define the maximum length of an object.

For example: mystringbuilder.capacity = 25;

3. The Ensurecapacity method can be used to check the capacity of the current StringBuilder. If the capacity is greater than the value passed, no changes are made, but if the capacity is less than the value passed, the current capacity is changed so that it matches the value passed.

4. You can also view or set the Length property. If the length property is set to a value greater than the capacity property, the capacity property is automatically changed to the same value as the Length property. If you set the Length property to a value that is less than the length of the string within the current StringBuilder object, the string is shortened.

Third, modify the StringBuilder string
The following table lists the methods that you can use to modify the contents of StringBuilder.

1. Stringbuilder.append appends the information to the end of the current StringBuilder.

For example:

StringBuilder Mystringbuilder = new StringBuilder ("Hello world!");
Mystringbuilder.append ("What a Beautiful Day.");
Console.WriteLine (Mystringbuilder);

This example sets the Hello world! What is a beautiful day. Display to the console.


2. Stringbuilder.appendformat replaces the format specifier passed in the string with the formatted text.

For example:

int MyInt = 25;
StringBuilder Mystringbuilder = new StringBuilder ("Your Total is");
Mystringbuilder.appendformat ("{0:c}", MyInt);
Console.WriteLine (Mystringbuilder);

This example displays Your total is $25.00 to the console.

3. Stringbuilder.insert inserts a string or an object into the current StringBuilder object at the specified index.

For example:

StringBuilder Mystringbuilder = new StringBuilder ("Hello world!");
Mystringbuilder.insert (6, "Beautiful");
Console.WriteLine (Mystringbuilder);

This example will be Hello Beautiful world! Display to the console.

4, Stringbuilder.remove removes the specified number of characters from the current StringBuilder object.

For example:

StringBuilder Mystringbuilder = new StringBuilder ("Hello world!");
Mystringbuilder.remove (5,7);
Console.WriteLine (Mystringbuilder);

This example displays the Hello to the console.

5. Stringbuilder.replace replaces the specified character at the specified index.

For example:

StringBuilder Mystringbuilder = new StringBuilder ("Hello world!");
Mystringbuilder.replace ('! ', '? ');
Console.WriteLine (Mystringbuilder);

This example will be Hello world? Display to the console.

The difference between StringBuilder and string

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.