Parsing the difference between string and StringBuilder in C #

Source: Internet
Author: User

  1. The disadvantage of string is that each time the contents of a string variable are changed, memory must be redistributed. You think, if you create a loop that iterates 100,000 times, each iteration connect prompt a word to a string, so that there are 100,000 strings in memory, and each string is only associated with a single character and has a significant performance impact. StringBuilder to address these issues by assigning a cache, which is a workspace, apply the StringBuilder class method to the Work Area Squadron string. Includes add, delete, remove, insert and replace characters, and more. After execution, the ToString method is called to convert the contents of the workspace to a string that is convenient for assigning to a string variable. This StringBuilder will improve some performance.

  2. The StringBuilder object is dynamic and can change the contents of a string. StringBuilder method to modify the contents of a string.

    1:stringbuilder.append (). Appends the content to the end of the current StringBuilder string.

    StringBuilder MYSB = new StringBuilder ("Hello!"); Mysb.append ("Hello everyone!")    "); Console.WriteLine (MYSB);

    2:stringbuilder.appendformat (). Use a formatted text instead of a string to pass the symbol.

    int Mymoney = 24;    StringBuilder MYSB = new StringBuilder ("The money owed to me is");    Mysb.appendformat ("{0:c}", Mymoney); Console.WriteLine (MYSB);

    3:stringbuilder.insert (int,string). Inserts a string and an object into the current StringBuilder string at the specified location.

    4:stringbuilder.remove (P,n). Removes the specified character from the current StringBuilder object, meaning that n characters are removed from the P position.

    5:stringbuilder.replace (A, B). is to replace A with B for the specified string, and A and B are both characters or strings.

    String A= "AAA AAA AAA";

    String B=a.replace (' A ', ' w ');

    Results output "www www www".

    The above methods are also used in string, even more than StringBuilder. The more out is as follows: PadRight ()/padleft () method, according to the specified width, with the corresponding number of the given word Fu Tiancai character, if no padding character is specified, fill with white space.

    String a= "ABCDEFG";

    A=a.padright (a.length+3, "!");

    The result outputs "ABCDEFG!!!".

    Split (char[]) method, the char array contains separators that can be used to split strings into substrings that are returned as elements in an array of strings.

    The Substring () method extracts the specified length string starting at the specified position n and, if specified as 1, extracts a substring of length 1.

    ToUpper ()/tolower () that returns a copy of the case of the string.

    Trim ()/trim (char[]), removes all whitespace characters from the string, and if provided is a char array, deletes the leading and trailing characters specified by this char array.

    TrimEnd (char[])/trimstart ([]), removes all leading and trailing characters specified by the char number, and removes the white space character if the array is null.

    ToCharArray ()/tochararray (n,1), extracts the character from the string and puts it in a Unicode character array

    The String object is immutable. Each time you use one of the methods in the System.String class, you create a new string object in memory, which requires a new space to be allocated for the new object. The overhead associated with creating a new string object can be very expensive in situations where you need to perform repeated modifications to the string.

    If you want to modify a string without creating a new object, you can use the System.Text.StringBuilder class.
    For example, using the StringBuilder class can improve performance when you concatenate many strings together in a loop.

Parsing the difference between string and StringBuilder in C #

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.