Performance of String and StringBuilder

Source: Internet
Author: User

Comparing the performance of String and StringBuilder, we can find a lot on the Internet, but most of them compare the connection performance of String. The efficiency of String connection String + = is indeed less than that of StringBuilder, you can find many examples online. In this way, we are assimilated by these tie evidence. We began to use StringBuilder in flood mode. StringBuilder is good for B String, and some friends even went further to encapsulate StringBuilder and reload operators to create classes, to replace String.

But not some String operations are not as good as StringBuilder?

Differences between String and StringBuilder:

1. Although both are reference types, String in C # is usually treated as a struct. For example, we write:

String text = "abc"; text. Replace ("a", "B"); is it true that the value of text changes? Obviously, we have to write:

Text = text. Replace ("a", "B"); are we aware of the String as a function parameter?

If you change to StringBuilder, you do not need the code in step 3.

2. The methods of String and StringBuilder are not exactly the same. This is nonsense. Of course it won't be the same. Well, they include the same and similar functions.

In the past, I learned that StringBuilder is very efficient in connecting strings. Since then, I love it. As long as the String method is available in StringBuilder, it will give priority to StringBuilder, without asking why, there is a belief in my mind. StringBuilder is really a man, really man!

Here, I began to have some doubts. Is the StringBuilder method better than the String method? Exploration spirit, yeah!

I use VS2008. See the following 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.