Difference between string and stringBuilder

Source: Internet
Author: User

String object

It cannot be changed. Every time you use a method in the System. String class, you must create a new String object in the memory, which requires a new space for the new object. If you need to modify the String repeatedly, the system overhead associated with creating a new String object may be very expensive. If you want to modify the string without creating a new object, you can use the System. Text. StringBuilder class.


StringBulider class

Variable character string. This class cannot be inherited.

Namespace:System. Text

For example, when many strings are connected together in a loop, using the StringBuilder class can improve performance.

By initializing variables using an overloaded constructor method, you can create a new instance of the StringBuilder class, as described in the following example.

Set capacity and length

Although the StringBuilder object is a dynamic object that allows you to expand the number of characters in its encapsulated string, you can specify a value for the maximum number of characters it can accommodate. This value is called the capacity of the object and should not be confused with the string length of the current StringBuilder object. For example, you can create a new instance of the StringBuilder class with the string "Hello" (Length: 5) and specify the maximum capacity of this object to 25. When you modify StringBuilder, it does not re-allocate space for itself until the capacity is reached. When the capacity is reached, a new space is automatically allocated and the capacity is doubled. 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 white spaces.

MyStringBuilder. Capacity = 25;

The EnsureCapacity method can be used to check the current capacity of the StringBuilder. If the capacity is greater than the passed value, no changes are made. However, if the capacity is smaller than the passed value, the current capacity is changed to match the passed value.

You can also view or set the Length attribute. If you set the Length attribute to a value greater than the Capacity attribute, the Capacity attribute is automatically changed to the same value as the Length attribute. If you set the Length attribute to a value smaller than the string Length in the current StringBuilder object, the string is shortened.

Modify StringBuilder string

The methods that can be used to modify the content of StringBuilder are listed below.

StringBuilder. Append: Append the information to the end of the current StringBuilder.

StringBuilder. AppendFormat replaces the format specifier passed in the string with formatted text.

StringBuilder. Insert inserts a string or object into the specified index of the current StringBuilder object.

StringBuilder. Remove removes a 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 the string representation of a text or object to the end of a string represented by the current StringBuilder object. In the following example, A StringBuilder object is initialized as "Hello World" and some text is appended to the end of the object. Space will be automatically allocated as needed.

Standard Format String. You can use this method to customize the variable format and append these values to the end of StringBuilder. The following example uses the AppendFormat method to place an integer set to the currency value format at the end of StringBuilder.

Int MyInt = 25;
StringBuilder MyStringBuilder =>

Standard Format String. You can use this method to customize the variable format and append these values to the end of StringBuilder. The following example uses the AppendFormat method to place an integer set to the currency value format at the end of StringBuilder.

Int MyInt = 25;
StringBuilder MyStringBuilder =>

Remove

You can use the Remove method to Remove a specified number of characters from the current StringBuilder. The removal process starts from the specified index starting from scratch. The following example uses the Remove Method to shorten StringBuilder. Corresponding to Insert

Based on the above Code, we found that a new class, stringBulider, has a lot of familiarity with the previously learned string class or some functional styles in vb. You just want to use the formate function or the function in vb to intercept the string. Therefore, summing up previous learning has a lot to do in learning.

Now let's talk about the role of the StringBulider function in our system. Because stringBulider can replace and append methods. Therefore, you can run this method class to remove the non-conforming and replaced with the standard type.



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.