How to use Android StringBuffer

Today, let's talk about the use of StringBuffer.

Like string, StringBuffer class is also used to represent strings. However, because the internal implementation of StringBuffer is different from string, StringBuffer does not generate new objects during string processing, and its memory use is better than that of string class.

Therefore, in actual use, if you often need to modify a string, such as insert, delete and other operations, using StringBuffer is more suitable.

There are many methods in the StringBuffer class that are the same as those in the string class. The functions of these methods are exactly the same as those in the string class.

However, one of the most significant differences is that each modification of the StringBuffer object will change the object itself, which is the biggest difference from the string class.

In addition, because StringBuffer is thread safe, the concept of thread is introduced in the following chapters, so it can also be used conveniently in multithreaded programs, but the execution efficiency of the program is relatively slow.

1. Initialization of StringBuffer object

Unlike the initialization of the string class, the initialization of the StringBuffer object is provided by java with a special syntax. In general, the constructor is used for initialization.

For example:

The StringBuffer object initialized in this way is an empty object.

If you need to create a StringBuffer object with content, you can use:

The content of the initialized StringBuffer object is the string "ABC".

It should be noted that StringBuffer and string belong to different types and cannot be cast directly. The following codes are wrong:

The code of inter conversion between StringBuffer object and string object is as follows:

2. Common methods of StringBuffer

The methods in the StringBuffer class mainly focus on string changes, such as append, insert and delete. This is also the main difference between the StringBuffer class and the string class.

a. Append method

This method is used to append content to the end of the current StringBuffer object, similar to string connection. After calling this method, the contents of the StringBuffer object are also changed, for example:

The value of object sb will become "abctrue".

Using this method to connect strings will save more content than string. For example, it is applied to the connection of database SQL statements, such as:

Thus, the value of the object sb is the string "select * from userinfo where username = test and PWD = 123".

b. Deletecharat method

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

The function of this code is to delete the character with the index value of 1 in the string object sb, that is, delete the second character, and the remaining contents form a new string. So the value of object sb becomes "TST".

There is also a delete method with similar functions:

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

The function of this code is to delete all characters from index value 1 (including) to index value 4 (excluding), and the remaining characters form a new string. The value of object sb is "tstring".

c. Insert method

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

The function of the sample code is to insert the false value at the index value 4 of the object sb to form a new string. After execution, the value of the object sb is "testfalse string".

d. Reverse method

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

After inversion, the content in the object sb will become "CBA".

e. Setcharat method

This method is used to modify the character whose index value is the index position in the object to a new character ch. For example:

The value of object sb will become "ADC".

f. Trimtosize method

The function of this method is to reduce the storage space in the StringBuffer object to the same length as the string length, so as to reduce the waste of space.

In short, in actual use, string and StringBuffer have their own advantages and disadvantages. You can select the corresponding type according to the specific use environment.

In addition to the above knowledge, string has three ways to change the content,

StringBuffer by using sbi.setlength (0); To empty the contents of the StringBuffer object is the most efficient.

The above is a detailed explanation of the use of Android StringBuffer introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>