. NET StringBuilder Usage Example Analysis _ practical skills

Source: Internet
Author: User

This example describes the. NET in StringBuilder usage. Share to everyone for your reference. The specific analysis is as follows:

Why to use StringBuilder

Why use StringBuilder to start with the attributes of a string object.
String objects are concatenation, because the string is not denatured, the string object will be spliced each time, will copy a copy out of the operation, while the string is still in memory, a large number of temporary fragments can cause negligible performance loss. So it is recommended to use StringBuilder in a large number of string concatenation
the simple way of using StringBuilder:

Copy Code code as follows:
string S1 = "33";
String s2 = "44";
String s3 = "55"; The demand is to put the S1 S2 S3 stitching together. This is a typical string concatenation.
With StringBuilder, no unwanted temporary strings are generated.
StringBuilder SB =new StringBuilder ();
Stitching Way 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 approach.
Chained programming is often the way in jquery
Sb. Append (S1). Append (S2). Append (S3);
Finally put SB. ToString () will be fine.

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

I hope this article will help you with your. NET programming.

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.