StringBuilder in C #

Source: Internet
Author: User

1. Benefits of using StringBuilder

   Because the string object is immutable, each time we use the method of the string class, we re-create a new string object in memory and allocate memory space for that object. If the string object is modified frequently in a piece of code, the memory consumption will be significant. For example, in a loop, where string concatenation is performed frequently, if the loop executes multiple times, the string generated by each concatenation of strings will consume a lot of memory space. So, in order to improve the performance of the program, we use the System.Text.StringBuilder class to deal with the need to modify the string instead of recreating the string.

  StringBuilder is a container that is used to hold mutable strings, and is a dynamic string object.

2. Create StringBuilder

            New StringBuilder ();             New StringBuilder (// set Max capacity of StringBuilder object            new StringBuilder (" abcdef ");

3. features of the StringBuilder

1. The capacity of the object is size

The maximum number of characters that can be accommodated is changed, and can be changed by setting the capacity property. It does not automatically reallocate space until capacity is reached, and when it reaches capacity, it automatically allocates new space and doubles the capacity.

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.

2. The size of the saved string

The length property of the string that is accommodated can also be viewed or set. 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.

3. You can use ToString () to convert the buffer data to a string

4. Common methods of StringBuilder

  

  (1) 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.

            New StringBuilder ("Hello World  . ") " );            Strbuilder.append ("  I love China". " // Output: Hello  World. I love China.            Strbuilder.append ("123"). Append ("456"// output: Hello world  . I Love china.123456

(2) AppendFormat   method adds text to the end of   StringBuilder  , and implements the   IFormattable   Interface, so the standard format string described in the format section can be accepted. You can use this method to customize the format of a variable and append these values to   StringBuilder   after. The following example uses the   AppendFormat   method to place an integer value formatted as a currency value to the end of   StringBuilder  .  

            int  $ ;             New StringBuilder ("");            Mystringbuilder.appendformat ("{0:c}  ", Mycash);            Console.WriteLine (Mystringbuilder); // Your Total is $200.00

  (3) 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.

            New StringBuilder ("Hello World  . ") " );            Strbuilder1.insert (6"Beautiful");            Console.WriteLine (strBuilder1); // Hello Beautiful World.

  (4) Remove removes the specified number of characters from the current StringBuilder using the Remove method, starting at the specified zero-based index. The following example uses the Remove method to shorten the StringBuilder.

            New StringBuilder ("Hello  world! " );            Strbiulder2.remove (57);            Console.WriteLine (strBiulder2); // Hello

(5) Replace using the Replace method, you can replace the characters within the StringBuilder object with another specified character.   The following example uses the Replace method to search for a StringBuilder object, find all the exclamation-point characters (!), and use the question mark character (?). To replace them.

            New StringBuilder ("Hello  world! " );            Strbuilder3.replace ('! ' ' ? ' );            Console.WriteLine (STRBUILDER3); // Hello  the world?

4. StringBuilder VS String

  In terms of operational performance and memory efficiency, StringBuilder is much better than string, avoiding the creation of too many temporary string objects, especially in situations where modifications are frequently repeated. On the other hand, the string class provides more ways to enable development to implement applications faster.

on the choice of both, if the application is more stringent for system performance, memory requirements, and often handles large-scale strings, recommended use of StringBuilder objects; For example, when working with a database, we need to splice SQL statements frequently. Otherwise, you can choose to use String.

StringBuilder in C #

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.