The difference between the StringBuilder class and the String class in C #---

Source: Internet
Author: User

When looking for a job, go to some companies, avoid the interview and written test. But generally the first is the written examination. I have a problem with this: what is the difference between a StringBuilder class and a string class? I was not quite sure about the difference between the two classes, and today, when we look at the code, See colleagues also used the StringBuilder class. So I went online to check the data, but also want to summarize the difference between the StringBuilder class and the string class. People who learn computer language must understand oh, maybe the day you go to find a job, you will encounter this problem.

The String object is immutable. Each time you use one of the methods in the System.String class or perform an operation (such as assignment, stitching, and so on), you create a new string object in memory, which requires a new space to be allocated for the new object. And the StringBuilder won't. 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.

1. It is a reference type that allocates memory on the heap

2. A new instance is generated when the operation is

3.String object once generated immutable (immutable)

3. Define equality operators (= = and! =) to compare the value of a String object (not a reference)

The following is a description of a StringBuilder class for copying:

[C #]
StringBuilder Mystringbuilder = new StringBuilder ("Hello world!");
Set capacity and length
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.
[C #]
StringBuilder Mystringbuilder = new StringBuilder ("Hello world!", 25);
In addition, you can use the read/write capacity property to set the maximum length of an object. The following code example uses the capacity property to define the maximum length of an object.
[C #]
Mystringbuilder.capacity = 25;
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.
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.
Modifying the StringBuilder string
The following table lists the methods that you can use to modify the contents of StringBuilder.
Method name Use
Stringbuilder.append appends the information to the end of the current StringBuilder.
Stringbuilder.appendformat replaces the format specifier passed in the string with the formatted text.
Stringbuilder.insert inserts a string or an object into the current StringBuilder object at the specified index.
Stringbuilder.remove removes the specified number of characters from the current StringBuilder object.
Stringbuilder.replace replaces the specified character at the specified index.
Append
The Append method can be used to add a string representation of text or an object to the end of a string represented by the current StringBuilder object. The following example initializes a StringBuilder object to "Hello world" and then appends some text to the end of the object. The space is automatically allocated as needed.
[C #]
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.
AppendFormat
The AppendFormat method adds text to the end of StringBuilder and implements the IFormattable interface, so the standard format string described in the formatting section can be accepted. You can use this method to customize the format of a variable and append these values to the back of the StringBuilder. The following example uses the AppendFormat method to place an integer value formatted as a currency value to the end of StringBuilder.
[C #]
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.
Insert
The Insert method adds a string or object to the specified position in the current StringBuilder. The following example uses this method to insert a word into the sixth position of the StringBuilder.
[C #]
StringBuilder Mystringbuilder = new StringBuilder ("Hello world!");
Mystringbuilder.insert (6, "Beautiful");
Console.WriteLine (Mystringbuilder);
This example will be Hello Beautiful world! Display to the console.
Remove
You can use the Remove method to remove a specified number of characters from the current StringBuilder, and the removal process starts at the specified zero-based index. The following example uses the Remove method to shorten the StringBuilder.
[C #]
StringBuilder Mystringbuilder = new StringBuilder ("Hello world!");
Mystringbuilder.remove (5,7);
Console.WriteLine (Mystringbuilder);
This example displays the Hello to the console.
Replace
Using the Replace method, you can replace characters within a StringBuilder object with another specified character. The following example uses the Replace method to search the StringBuilder object, finds all the exclamation-point characters (!), and replaces them with the question mark character (?).
[C #]
StringBuilder Mystringbuilder = new StringBuilder ("Hello world!");
Mystringbuilder.replace ('! ', '? ');
Console.WriteLine (Mystringbuilder);

The difference between a StringBuilder class and a String class in C #---(GO)

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.