Usage of Java StringBuilder

Source: Internet
Author: User

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.

(i) Set capacity and length
Although the StringBuilder object is a dynamic object, it allows you to expand the number of characters in the string it encapsulates, but you can specify a value for the maximum number of characters it can hold. This value is called the capacity of the object and should not be confused with the length of the string that the current StringBuilder object holds. For example, you can create a new instance of the StringBuilder class with the string "Hello" (length 5), and you can specify that the object has a maximum capacity of 25. When StringBuilder is modified, it does not redistribute space for itself until the capacity is reached. When the capacity is reached, new space is automatically allocated and the capacity doubles. You can use one of the overloaded constructors to specify the capacity of the StringBuilder class. The following code example specifies that the Mystringbuilder object can be expanded to a maximum of 25 blanks.
Stringbuildermystringbuilder = new StringBuilder ("Hello world!", 25);
In addition, you can use the read/write capacity property to set the maximum length of an object. The following code example uses the capacity property to define the maximum length of an object.
Mystringbuilder.capacity= 25;

(ii) Several common methods of this type are listed below:
(1) The Append method can be used to add a string representation of text or an object to the end of a string represented by the current StringBuilder object. The following example initializes a StringBuilder object to "Hello world" and then appends some text to the end of the object. The space is automatically allocated as needed.
Stringbuildermystringbuilder = new StringBuilder ("Hello world!");
Mystringbuilder.append ("What a Beautiful Day.");
Console.WriteLine (Mystringbuilder);
This example sets the Hello world! What Abeautiful day. Displays to the console.

(2) The AppendFormat method adds text to the end of StringBuilder and implements the IFormattable interface, so the standard format string described in the formatting section can be accepted. You can use this method to customize the format of a variable and append these values to the back of the StringBuilder. The following example uses the AppendFormat method to place an integer value formatted as a currency value to the end of StringBuilder.
int myint= 25;
StringBuilder Mystringbuilder = new StringBuilder ("Your Total is");
Mystringbuilder.appendformat ("{0:c}", MyInt);
Console.WriteLine (Mystringbuilder);
This example displays Your total is $25.00 to the console.

(3) The Insert method adds a string or object to the specified position in the current StringBuilder. The following example uses this method to insert a word into the sixth position of the StringBuilder.
Stringbuildermystringbuilder = new StringBuilder ("Hello world!");
Mystringbuilder.insert (6, "Beautiful");
Console.WriteLine (Mystringbuilder);
This example displays the Hello beautifulworld! to the console.

(4) The Remove method can be used to remove a specified number of characters from the current StringBuilder and the removal process starts at the specified zero-based index. The following example uses the Remove method to shorten the StringBuilder.
Stringbuildermystringbuilder = new StringBuilder ("Hello world!");
Mystringbuilder.remove (5,7);
Console.WriteLine (Mystringbuilder);
This example displays the hello to the console.

(5) Using the Replace method, you can replace the characters within the StringBuilder object with another specified character. The following example uses the Replace method to search for a StringBuilder object, find all the exclamation-point characters (!), and use the question mark character (?). To replace them.
Stringbuildermystringbuilder = new StringBuilder ("Hello world!");
Mystringbuilder.replace ('! ', '? ');
Console.WriteLine (Mystringbuilder);

Thanks http://blog.csdn.net/zi_jun/article/details/7624999

Usage of Java 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.