Java String Collation Note (iii)

Source: Internet
Author: User

5. Using the StringBuilder class

String the length of the string object is fixed, and its contents cannot be changed and edited. Each time a new string is concatenated and generated using the "+" connector, a new string object is created in memory and a new space is allocated for the object, which

The system overhead is greatly increased in the case of repeated modifications to the string. You can use the StringBuilder class to dynamically change the contents of a string, and if you have a string operation that you want to perform frequently, using StringBuilder will make the execution of the string much more efficient.

5.1. Create a string generator

5.1.1. StringBuilder () Construction method

The construction method creates an empty string generator. The initial capacity of the newly created StringBuilder object is 16 characters. The syntax is as follows:

New StringBuilder () // New:java language keyword for creating new objects

5.1.2. StringBuilder (int capacity) Construction method

The construction method creates a string generator that specifies the initial space size. The syntax is as follows:

New StringBuilder (capacity) // New:java language Keywords // capacity: This is an integer number that defines the initial capacity of the string generator

For example, the code for creating a string generator object with a capacity of 15 is as follows:

StringBuilder builder=New StringBuilder (15);

5.1.3. StringBuilder (String str) Construction method

The construction method creates and initializes a string generator with the specified string, whose initial content is the contents of the specified string. The syntax is as follows:

New StringBuilder (String str) // New:java language keyword for creating new objects // STR: This is a string that will be the initial content of the newly created string generator

For example, the code for creating a string generator object with the "Great Wall of China" as its initial value is as follows:

StringBuilder builder=New StringBuilder ("Great Wall of China");

5.2. Application of String generator

The StringBuilder class provides a number of methods for dynamically performing editing operations such as adding, deleting, inserting, and so on. The common methods of this class are as follows.

5.2.1. Append () method

This method is used to append content to the string generator, which has several overloaded implementations that can accept any type of data, such as int, string, double, or another string generator. The syntax is as follows:

Append (content) // content: This parameter is intended to be appended to the string generator. 

5.2.2. Delete (int start,int end) method

This method is used to remove substrings from the string generator from the specified range (start to end-1). The syntax is as follows:

Delete (start,end) // start: The starting position of the substring to delete // end: The end position of the substring to delete

For example, to remove "FirstName" from a full name, you can call this method with the following code:

 Public class Test {    publicstaticvoid  main (string[] args) {        // TODO auto-generated Method stub        StringBuilder builder=New StringBuilder ("zhangxiaoming");        // Creating a String generator        Builder.delete (0, 5);            // Delete substring        System.out.println (builder);    // Output String     }}

Output after executing code: xiaoming.

5.2.3. Deletecharat (int index) method

This method deletes the character at the specified index in the specified string generator. The syntax is as follows:

Deletecharat (Index) // Index: The indexed position of the character to be removed in the string builder

5.2.4. Insert (int offset,arg)

This method is used to insert data content into the location specified in the string builder, such as int, float, char, or other objects. The syntax is as follows:

Insert (OFFSET,ARG) // offset: Position in the string builder // ARG: The data content that will be inserted into the string builder, which can be of any type. 

5.2.5. Length () method

This method is used to return the length of the content in the string generator. The syntax is as follows:

Length ()

Example 1.9 demonstrates the difference between a string and a StringBuilder, with the following code:

 Public classTest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubString teststr=NewString ("Java");//defining a String class objectString teststr2=teststr+ "Tutorial";//adding data to a stringStringBuilder builder=NewStringBuilder ("Java");//defining StringBuilder ObjectsStringBuilder builder2=builder.append ("tutorial");//Append content to the stringSYSTEM.OUT.PRINTLN ("Connect using a String object:" +TESTSTR2);//output String Object contentsSYSTEM.OUT.PRINTLN ("Connect using StringBuilder:" +builder2); }}

The result of the execution is: Java tutorial. Although the results are the same, the object contents of the string object are changed, and for the StringBuilder object, each time the contents of the StringBuilder are changed, the strings themselves are manipulated.

    

Java String Collation Note (iii)

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.