Java StringBuffer Class

Source: Internet
Author: User

If you often need to modify a string, such as inserting, deleting, stitching, and so on, it is more appropriate to use Sringbuffer, because stringbuffer in string processing is not to generate new objects in memory because of the string class.

The StringBuffer class has many of the same methods as the string class, which are functionally the same as in string, and StringBuffer is thread-safe and can be used conveniently in multithreaded programs. But the execution efficiency of the program is relatively slow.

1. Initialization of the StringBuffer object

The constructor is typically used to initialize Strinbuffer SB = new StringBuffer (); C at this point the SB is an empty STRINGB object. You can also use stringbuffer SB = new StringBuffer ("abc"); At this point the content of SB is the string "abc"

2. StringBuffer and string are of different types and cannot be coerced, the following code is wrong:

StringBuffer s = "abc"; Assignment types do not match

StringBuffer s = (stringbuffer) "ABC"; There is no inheritance relationship and cannot be strongly turned

3. The conversion between StringBuffer and string is as follows:

String s = "abc";

StringBuffer sb = new StringBuffer ("123");

Stirngbuffer sb1 = new StringBuffer (s); Convert string to StringBuffer;

String S1 = sb1.tostring (); StringBuffer Convert to String

4. Common methods of StringBuffer

The methods in the StringBuffer class mainly focus on the change of strings: Append, insert, delete, etc.

  

A, Append method

Public StringBuffer Append (Boolean B)

The function of this method is to append the content to the end of the current StringBuffer object, similar to a string connection. After the method is called, the contents of the StringBuffer object also change, for example:

StringBuffer sb = new StringBuffer ("abc");

Sb.append (TRUE);

The value of the object SB will become "Abctrue".

Using this method to concatenate strings will save more content than string, such as a connection applied to a database SQL statement, such as:

StringBuffer sb = new StringBuffer ();

String user = "Test";

String pwd = "123";

Sb.append ("select * from UserInfo where username=")

. Append (user)

. Append ("and pwd=")

. append (PWD);

The value of this object SB is the string "select * from UserInfo where username=test and pwd=123".

B, Deletecharat method

Public stringbuffer deletecharat (int index)

The function of this method is to delete the character at the specified position and then form the remaining content into a new string. For example:

StringBuffer sb = new StringBuffer ("Test");

SB. Deletecharat (1);

The function of the code is to delete the string object SB in the index value of 1 characters, that is, to delete the second character, the remaining content constitutes a new string. So the value of the object SB becomes "Tst".

There is also a function-like Delete method:

Public StringBuffer Delete (int start,int end)

The function of this method is to delete all characters within the specified interval, including the start, which does not contain the range of the end index value. For example:

StringBuffer sb = new StringBuffer ("teststring");

SB. Delete (1,4);

The purpose of the code is to remove all characters between the index value 1 (including) to the index value 4 (not included), and the remaining characters to form a new string. The value of the object SB is "Tstring".

C, Insert method

Public stringbuffer Insert (int offset, Boolean b)

The function of this method is to insert content into the StringBuffer object and then form a new string. For example:

StringBuffer sb = new StringBuffer ("teststring");

Sb.insert (4,false);

The purpose of the sample code is to insert a false value at the index value 4 of the object SB, to form a new string, then the value of the object SB after execution is "testfalsestring".

D, Reverse method

Public StringBuffer Reverse ()

The function of this method is to invert the contents of the StringBuffer object and then form a new string. For example:

StringBuffer sb = new StringBuffer ("abc");

Sb.reverse ();

After the reversal, the content in the object SB becomes "CBA".

E, Setcharat method

public void Setcharat (int index, char ch)

The function of this method is to modify the character of the index value in the object to be the new character Ch. For example:

StringBuffer sb = new StringBuffer ("abc");

Sb.setcharat (1, ' D ');

The value of the object SB will become "ADc".

F, TrimToSize method

public void TrimToSize ()

The purpose of this method is to reduce the space waste by narrowing the storage space of the StringBuffer object to the same length as the string length.

In short, in the actual use, string and stringbuffer each have advantages and disadvantages, according to the specific use of the environment, select the corresponding type to use.

Java StringBuffer Class

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.