. NET in the StringBuilder

Source: Internet
Author: User

Why to use StringBuilder

Why use StringBuilder to start with the properties of a string object.

string object when string stitching, Because of the immutability of the string, the string object will be duplicated every time, copying a copy out of the operation, and its own strings remain in memory, a large number of temporary fragments will cause negligible performance loss. Therefore, it is recommended to use StringBuilder


the simple way to use StringBuilder

string S1 = "33";

String s2 = "44";

String s3 = "55"; The demand is to S1 S2 S3 together. This is a typical string concatenation.

With StringBuilder, no useless temporary strings are generated.

StringBuilder SB =new StringBuilder ();

Stitching method One

Sb. Append (S1);

Sb. Append (S2);

Sb. Append (S3);

//splicing mode two  

Because the Append () method returns a This, which is the object itself. So you can use this method.

Chained programming jquery is commonly used in this way

Sb. Append (S1). Append (S2). Append (S3);

Finally put SB. ToString () Just a little bit better.

The Ps:appendline () method can automatically add a carriage return.

. NET in the StringBuilder

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.