C#string & StringBuilder

Source: Internet
Author: User

When looking at J2SE, see the word string again, in fact, in the process of C # system practice has been used several times:

The biggest difference between string and StringBuilder is the variable immutable problem, in fact, there are many pairs of comparisons in the data type, such as the perfect contrast between char and varchar.

String:

In the computer room charge system or student information management system, because the whole system is relatively small, so we use a string type at most, to complete our definition of the string. In fact, a string literal is a constant, immutable, and in the Java Memory Analysis perspective, it is necessary to reallocate memory every time the contents of a string change. Suppose a loop with a 10,000-time cycle, each iteration is reassigned, so the performance must not imaginable.

StringBuilder:

It is based on the perfect string, only to be used by people. StringBuilder learned to allocate a cache, which is a workspace to solve these problems, when the string size does not exceed capacity, will not allocate new capacity, when exceeded, will automatically increase the capacity, so that all the things done after the completion of the direct submission, it will improve performance.

Although the StringBuilder object is a dynamic object, the number of strings that are encapsulated is expanded, and this capacity is not confused with the length of the StringBuilder previously understood,

For Example:

You can create an example with the string "Friends" (length 7), and if I specify that the object has a maximum capacity of 25, then StringBuilder can expand the Mystringbuilder object to a maximum of 25 white space:

StringBuilder  mystringbuilder=new StringBuilder (hello,my dear friends! ", 25);
The maximum capacity can be defined like this:

Mystringbuilder.capacity = 25;
In our project, what we use most is still. Append:

StringBuilder strbuilder=new StringBuilder (); Stringbuilder.append (""); Stringbuilder.append (""); Stringbuilder.append (""); Stringbuilder.append ("");
The benefits of this use are also to improve performance.

in StringBuilder, several common methods are:

Stringbuilder.append (). Appends the content to the end of the current StringBuilder string. Eg:stringbuilder MYSB = new StringBuilder ("Hello!");    Mysb.append ("Hello everyone!") ");    Console.WriteLine (MYSB); Stringbuilder.appendformat (). Use a formatted text instead of a string to pass the symbol. Eg:int Mymoney =;    StringBuilder MYSB = new StringBuilder ("The money owed to me is");    Mysb.appendformat ("{0:c}", Mymoney);    Console.WriteLine (MYSB); Stringbuilder.insert (int,string). Inserts a string and an object into the current StringBuilder string at the specified location. Stringbuilder.remove (P,n). Removes the specified character from the current StringBuilder object, meaning that n characters are removed from the P position. Stringbuilder.replace (A, B). is to replace A with B for the specified string, and A and B are both characters or strings. Eg:string a= "AAA AAA AAA";    String B=a.replace (' A ', ' w ');    Results output "www www www".

In addition to these two, there is a stringbuffer, and StringBuilder compared to the biggest advantage is applicable to multi-threaded cases, summed up the use of the three:

1) If the amount of operation is less---String

2) manipulate large amounts of data---StringBuilder with single-threaded operation string buffers

3) Multi-threaded operation string buffer operation of large amounts of data---stringbuffer


C#string & StringBuilder

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.