String and StringBuilder

Source: Internet
Author: User

1) string is a reference type and cannot be modified once the string is created

For example:

String a= "AAA";

String B=a;

A is the same as the B value, but not the same object;

Each time you use a method in the System.String class, you create a new string object in memory and then allocate a new space for the new object. The system memory loss becomes larger when the string modification is more frequent.

2) StringBuilder is a variable sequence of characters that can be modified by an object of type StringBuilder, which points to an array of char structures, using StringBuilder to improve performance.

3) Common methods of StringBuilder

1:append (): Adds a string representation of text or an object to the end of the current StringBuilder object's string

StringBuilder sb = new StringBuilder ("I want to go shopping!") ");

Sb. Append ("The sun is good today,");

Console.WriteLine (SB);

2:appendformat (): Implements the IFormattable interface, which accepts a standard format string described in the Format section, using this method to customize the variable and append the value to the StringBuilder

int MyInt = 10;

StringBuilder Mystringbuilder = new StringBuilder ("The money is");

Mystringbuilder.appendformat ("{0:c}", MyInt);

Console.WriteLine (Mystringbuilder);

The money is $25.00;

3:remove (): You can use the Remove method to remove a specified number of characters from the current StringBuilder, and the removal process starts at the specified zero-based index.

StringBuilder Mystringbuilder = new StringBuilder ("Hello world!");

Mystringbuilder.remove (5,7);

Console.WriteLine (Mystringbuilder);

4:replace (): Using the Replace method, you can replace the characters within the StringBuilder object with another specified character.

StringBuilder Mystringbuilder = new StringBuilder ("Hello world!");

Mystringbuilder.replace ('! ', '? ');

Console.WriteLine (Mystringbuilder)

5:insert (): The Insert method adds a string or object to the specified position in the current StringBuilder.

StringBuilder Mystringbuilder = new StringBuilder ("Hello world!");

Mystringbuilder.insert (6, "Beautiful");

Console.WriteLine (Mystringbuilder)

String and 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.