C # series String and StringBuilder

Source: Internet
Author: User

Preface first, I would like to say hello to all of you at the Expo. I have been in the Expo for more than a year. I usually come over to read articles and learn from your experiences as soon as I have time. Now I am getting into the habit of not coming to the Expo Park for a day, and I am not so steadfast in my mind, I don't know if my big buddy is reading my blog post. I want to record what I normally read and what I read into my own understanding so that I can discuss it with colleagues. If you have a wrong understanding in my blog or do not fully understand it, please point out that the younger brother would like to thank you here first. Now let's talk about this. Let's start today's content. String: System. string class System. text. the StringBuilder class String string is one of the most commonly used types. It is a special reference type that is directly derived from objects. Therefore, its values are stored on the managed stack. When constructing a new string, you do not need to use new. Copy the code using System; class Program {static void Main (string [] args) {string s = "This is right"; // This is the correct Console. writeLine (s); string B = new String ("This is error"); // This is the error Console. writeLine (B) ;}} copies the code and it is "immutable". After the string object is initialized, the length and content of the string object remain unchanged. At this time, we need to change or add a string. What will happen? Assume that this piece of code copies the using System code; class Program {static void Main (string [] args) {string s = "I am"; s ++ = "Sky"; Console. writeLine (s); // my name is Sky} copy Code 1. Create a New String object B, and the runtime will allocate enough memory for the new object B, store the new content "I am Sky ". 2. The variable s points to the new object B. 3. The old object a is not referenced by any variable and is waiting for garbage collection. Because of the above features, to avoid copying multiple objects of the same string, the CLR string retention mechanism will be used to reduce memory consumption. What is string reserved for use? During CLR initialization, a hash table is created. Each new string created matches the hash table to check whether the same string exists. If yes, the existing old object is returned and referenced by the new variable. Otherwise, a copy of the string will be created and added to the hash table. The Key is the string, and the Value is the address of the string object on the stack. StringBuilder causes performance loss when modifying the string multiple times because of the "immutable" of string, so the role of StringBuilder is coming at this time. Compared with String, StringBuilder has fewer functions, including basic attributes and addition, deletion, and modification methods. In fact, StringBuilder is composed of Char arrays. When building a new StringBuilder, you can set the capacity. When you add a string that exceeds the size of the allocated array, a new array is created, which doubles the capacity, the old data will wait for garbage collection. Most StringBuilder Methods return the same StringBuilder object. At this time, several operations can be completed together. Copy the code using System; using System. text; class Program {static void Main (string [] args) {StringBuilder sb = new StringBuilder (10); string s = sb. appendFormat ("{0} {1}", "Skyer", "Chen "). remove (3, 2 ). replace ('','-'). toString (); Console. writeLine (s); // display result Sky-Chen} copy the code

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.