If our program is running under a single thread or does not have to consider thread synchronization issues, we should give priority to using the StringBuilder class; if we want to ensure thread safety, it is naturally a StringBuffer.
Except for the different support for multithreads, there is almost no difference in how and how these two classes are used.
The difference is that StringBufferd supports concurrent operations, is linear and safe, and is suitable for use in multithreading. StringBuilder does not support concurrent operations, is linear and unsafe, and is not suitable for use in multithreading.The newly introduced StringBuilder class is not thread-safe, but it performs better in single thread than StringBuffer.
Common methods of StringBuffer
(Since StringBuffer and StringBuilder are almost the same in use, only one is written. The following content is collected from all over the network and the source is no longer marked)
StringBuffer s = new StringBuffer();
The StringBuffer object initialized in this way is an empty object.
StringBuffer sb1=new StringBuffer(512);
A character buffer of 512 bytes in length is allocated.
StringBuffer sb2=new StringBuffer(“how are you?”)
Create a StringBuffer object with content, storing the string "how are you?" in the character buffer
a. Append method
public StringBuffer append(boolean b)
The function of this method is to append content to the end of the current StringBuffer object, similar to a string connection. After calling this method, the content of the StringBuffer object also changes, for example:
StringBuffer sb = new StringBuffer(“abc”); (true);
Then the value of object sb will become "abctrue"
Using this method to connect strings will be more content-saving than String and is often used for connections of database SQL statements.
b. deleteCharAt method
public StringBuffer deleteCharAt(int index)
The purpose of this method is to delete the characters at the specified position and then turn the remaining content into a new string. For example:
StringBuffer sb = new StringBuffer(“KMing”); sb. deleteCharAt(1);
The function of this code deletes the character with an index value of 1 in the string object sb, that is, deletes the second character, and the remaining content forms a new string. Therefore, the value of object sb becomes "King".
There is also a delete method with similar functions:
public StringBuffer delete(int start,int end)
The purpose of this method is to delete all characters within the specified interval, including start, and not end index value. For example:
StringBuffer sb = new StringBuffer(“TestString”); sb. delete (1,4);
The purpose of this code is to delete all characters between index value 1 (including) and index value 4 (excluding), and the remaining characters form a new string. Then the value of object sb is "TString".
c. insert method
public StringBuffer insert(int offset, boolean b),
The purpose of this method is to insert content into the StringBuffer object and then form a new string. For example:
StringBuffer sb = new StringBuffer(“TestString”); (4,false);
The purpose of this example code is to insert a false value at the index value 4 of object sb to form a new string, and then the value of object sb is "TestfalseString".
d, reverse method
public StringBuffer reverse()
The purpose of this method is to reverse the contents in the StringBuffer object and then form a new string. For example:
StringBuffer sb = new StringBuffer(“abc”); ();
After inversion, the content in object sb will become "cba".
e, setCharAt method
public void setCharAt(int index, char ch)The function of this method is to modify the index value in the object toindexThe character at the position is the new characterch。For example: StringBuffer sb = new StringBuffer(“abc”); (1,'D');
Then the value of object sb will become "aDc".
f, trimToSize method
public void trimToSize()
The function of this method is to reduce the storage space in the StringBuffer object to the same length as the string, and reduce the waste of space. It has the same function as String's trim(), and is not given an example.
g, length method
The purpose of this method is to get the string length, no need to say it.
h, setlength method
The purpose of this method is to set the string buffer size.
StringBuffer sb=new StringBuffer(); (100);
If the setlength() method is called with a value smaller than the current string length, the characters after the new length will be lost.
i. Method
The purpose of this method is to obtain the capacity of the string.
StringBuffer sb=new StringBuffer(“string”); int i=();
j, ensureCapacity method
The purpose of this method is to reset the size of the string capacity.
StringBuffer sb=new StringBuffer(); (32); //Pre-setsbThe capacity of32
k, getChars method
The purpose of this method is to copy the substring of the string to the array.
getChars(int start,int end,char chars[],int charStart); StringBuffer sb = new StringBuffer("I love You"); int begin = 0; int end = 5; //Note that the length of the ch character array must be greater than or equal to the length of the characters between begin and end//If it is less than, ArrayIndexOutOfBoundsException will be reported//If it is greater than, characters larger than will be filled with spaceschar[] ch = new char[end-begin]; (begin, end, ch, 0); (ch);
Results: I lov
The above is the StringBuffer and StringBuilder in Android introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!