String and stringbuffer

Source: Internet
Author: User

 

1. Relationship with string

A. There are two different classes. Once the string instance is created, the content cannot be modified. The stringbuffer instance can modify the collation columns contained in the instance.

B. conversion between the two:

* A string instance with the same character sequence can be constructed from a stringbuffer instance using the string-like constructor.

Format: New String (reference of the stringbuffer instance );

In addition, you can use the stringbuffer member method to complete the same conversion.

Example: Public String tostring ();

If B is a variable of the stringbuffer type, then: String S = string (B); and string S = B. tostring (); generates a string instance with the same content as the strngbuffer instance pointed to by B and points it TO THE STRING variable S.

* In turn, the stringbuffer instance with the same character sequence can be constructed from the string instance. The method is constructed using stringbuffer:

Public stringbuffer (string Str)

Example: stringbuffer B = new stringbuffer ("ABCD ");

2. Modify the stringbuffer instance content

A. Use the stringbuffer member method public int length () to obtain the string length of the stringbuffer instance.

B. Use the stringbuffer member method: Public char charat (INT index) to obtain the character specified by the character sequence.

C. Using the stringbuffer member method: Public void setcharat (INT index, char ch), you can set the character set in the Character Sequence of the stringbuffer instance to the specified character.

Example: stringbuffer B = new stringbuffer ("that ");

B. setcharat (0, 'C'); // Changes the Character Sequence of the stringbuffer force directed by B from that to Char.

D. Several common member methods for modifying stringbuffer instances

* Append: Add new content after the string Sequence

Example: stringbuffer B = new stringbuffer ("pretty ");

B. append ("Good! "); // The Character Sequence of B is changed to pretty good!

* Delete: Delete the specified sub-sequence in the string sequence.

Example: stringbuffer B = new stringbuffer ("those ")

B. Delete (2, 4); // Delete characters whose subscripts In the Character Sequence those are 2 and 3 (4-1 ).

* Insert: Insert new content to the character sequence at the specified position.

Example: stringbuffer B = new stringbuffer ("1 + = 2 ");

B. insert (); // insert an integer 1 before the subscript of "1 + = 2" is 2.

* Replace: Replace the specified subscript. The declaration method is public stringbuffer Replace (INT start, int end, string Str)

For example, stringbuffer B = new stringbuffer ("25 *?? = 625 ");

B. Replace (, "25"); // Replace the specified substring (from the subscript start to the subscript end-1) with the substring

* Reverse: converts the Character Sequence of the stringbuffer instance to its reverse character sequence. The declaration method is public stringbuffer reverse ()

Example: stringbuffer B = new stringbuffer ("pretty ");

B. Reverse (); // Changes the content of the stringbuffer instance directed by B from pretty to ytterp

3. Capacity of the stringbuffer instance: the memory occupied by the stringbuffer instance is called the capacity of the stringbuffer instance. You can use the stringbuffer member method public int capacity () to obtain the capacity of the instance.

A And stringbuffer have three constructor methods. The capacity of the stringbuffer instance generated by these three constructor methods is as follows:

Constructor

Character Sequence length

Memory Capacity

Public stringbuffer ()

0

16

Public stringbuffer (INT length)

0

Length

Public stringbuffer (string Str)

STR character Length

Length of the Character Sequence in STR plus 16

B. when the capacity of the current stringbuffer instance cannot accommodate the Character Sequence after the change, the stringbuffer instance will apply for memory again to increase the memory capacity. When the stringbuffer-like member method is called

Public void ensurecapacity (INT minimumcapacity). If the parameter minimymcapacity is larger than the current stringbuffer instance capacity, the stringbuffer instance will apply for memory again to increase the memory capacity. The new memory capacity is a large number of the following two parameters: ① minimumcapacity; ② (current stringbuffer instance capacity) * 2 + 2.

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.