Difference between string and stringbuilder

Source: Internet
Author: User

The string object cannot be changed. Each time you use one of the methods in the string class or perform operations (such as assignment and concatenation), You must create a new String object in the memory, in this case, you need to allocate a new space for the new object. Stringbuilder represents a variable character string. This class cannot be inherited. 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 system. text. stringbuilder class. For example, the stringbuilder class can be used to improve performance when many strings are connected in a loop.

1. Set the capacity and length

Stringbuilder mystringbuilder = new stringbuilder ("Hello world! ", 25); Method 1
Mystringbuilder. capacity = 25; method 2
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.

2. Modify the stringbuilder string

Use method name
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 remove 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. The following example initializes a stringbuilder object as "Hello World" and appends some text to the end of the object. Space will be automatically allocated as needed.

[C #]
Stringbuilder mystringbuilder = new stringbuilder ("Hello world! ");
Mystringbuilder. append ("What a beautiful day .");
Console. writeline (mystringbuilder );
 
In this example, hello World! What a beautiful day. displayed on the console.

Appendformat
The appendformat method adds text to the end of stringbuilder and implements the iformattable interface. Therefore, the standard format string described in the formatting section is acceptable. 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.

[C #]
Int Myint = 25;
Stringbuilder mystringbuilder = new stringbuilder ("Your total is ");
Mystringbuilder. appendformat ("{0: c}", Myint );
Console. writeline (mystringbuilder );

In this example, the your total is $25.00 is displayed on 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 to the sixth position of stringbuilder.

[C #]
Stringbuilder mystringbuilder = new stringbuilder ("Hello world! ");
Mystringbuilder. insert (6, "beautiful ");
Console. writeline (mystringbuilder );
 
In this example, hello beautiful world! Displayed on the console.

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.

[C #]
Stringbuilder mystringbuilder = new stringbuilder ("Hello world! ");
Mystringbuilder. Remove (5, 7 );
Console. writeline (mystringbuilder );

In this example, hello is displayed on the console.

Replace uses the replace method to replace the characters in the stringbuilder object with another specified character. The following example uses the replace method to search for stringbuilder objects and find all exclamation point characters (!), Use the question mark character (?) To replace them.

[C #]
Stringbuilder mystringbuilder = new stringbuilder ("Hello world! ");
Mystringbuilder. Replace ('! ','? ');
Console. writeline (mystringbuilder );

In this example, hello World? Displayed on the console.

3. Convert the stringbuilder string to the string form.

String = stringbuilder. tostring ();

Instance:

string text = "";StringBuilder sbi = new StringBuilder("");DateTime s1 = System.DateTime.Now;for (int i = 0; i < 50000; i++){sbi.Append(i);}DateTime s11 = System.DateTime.Now;Console.WriteLine(s11-s1);s1 = System.DateTime.Now;for (int i = 0; i < 50000; i++){text = text + i;}s11 = System.DateTime.Now;Console.WriteLine(s11 - s1);

  

 

 

 

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.