So in practical use, if you often need to modify a string, such as insertion and deletion, then it is more appropriate to use StringBuffer.
There are many methods in the StringBuffer class that are the same as those in the String class, and 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, it can be easily used in multithreaded programs, but the execution efficiency of the programs is relatively slow.
1, StringBuffer object initialization
Unlike the initialization of String class, the initialization of StringBuffer object has a special syntax provided by Java, but it is usually initialized by a constructor.
For example:
1
string buffer s = new string buffer();
The StringBuffer object initialized in this way is an empty object.
If you need to create a StringBuffer object with content, you can use:
1
string buffer s = new string buffer(" ABC ");
The content of the StringBuffer object initialized in this way is the string "abc".
It should be noted that StringBuffer and String belong to different types and cannot be directly cast. The following codes are all wrong:
1
2
StringBuffer s = "abc// Assignment types do not match.
string buffer s =(string buffer)" ABC "; //There is no inheritance relationship, and forced transfer cannot be performed.
The code for mutual conversion between StringBuffer object and String object is as follows:
1
2
three
four
String s = " abc
string buffer sb 1 = new string buffer(" 123 ");
StringBuffer sb2 = new stringbuffer; //the string is converted to StringBuffer.
string s 1 = sb 1 . tostring(); //StringBuffer is converted into a string.
2. Common methods of 2.StringBuffer
The methods in the StringBuffer class are mainly aimed at the changes of strings, such as adding, inserting and deleting, which is also the main difference between StringBuffer and String class.
A. Additional methods
1
Public stringbuffer District Addition (Boolean B)
The function of this method is to append the content to the end of the current StringBuffer object, similar to string concatenation. After calling this method, the contents of the StringBuffer object will also change, for example:
1
2
string buffe sb = new string buffer(" ABC ");
Sb.append (true);
The value of the object sb becomes "abctrue".
Using this method to connect strings will save more content than strings, for example, it is applied to the connection of database SQL statements, such as:
1
2
three
four
five
six
seven
string buffer sb = new string buffer();
String user = " test
string pwd = " 123 ";
sb . append(" select * from userInfo where username = ")
. Append (user)
. append(" and pwd= ")
. Add (pwd);
So the value of object sb is a string.
1
Select * from userInfo, where username=test, pwd= 123.
B. Method of deleting characters
1
Public string buffer delete charat (int index)
The function of this method is to delete the characters at the specified position, and then compose the remaining contents into a new string. For example:
1
2
string buffer sb = new string buffer(" Test ");
sb . delete charat( 1);
The function of this code is to delete the character with the index value of 1 in the string object sb, that is, to delete the second character, and the rest will form a new string. The value of the object sb becomes "Tst".
There is another deletion method with similar functions:
1
Delete public stringbuffer district (int start, int end)
The function of this method is to delete all characters in the specified interval, including the interval with the starting index value and the interval without the ending index value. For example:
1
2
string buffer sb = new string buffer(" test string ");
sb.delete ( 1,4);
The function of this code is to delete all characters from index value 1 (inclusive) to index value 4 (exclusive), and the remaining characters form a new string. The value of the object sb is "TString".
C. insertion method
1
Common StringBuffer insert(int offset, boolean b)
The function of this method is to insert the content into the StringBuffer object, and then form a new string. For example:
1
2
string buffer sb = new string buffer(" test string ");
Sb.insert(4, false);
The function of this sample code is to insert a false value at the index value 4 of the object sb to form a new string, and then the value of the object sb is "TestfalseString" after execution.
D. Reverse method
1
Public stringbuffer inversion ()
The function of this method is to reverse the contents of the StringBuffer object and then form a new string. For example:
1
2
string buffer sb = new string buffer(" ABC ");
sb . revert();
After inversion, the content in the object sb will become "cba".
E, setCharAt method
1
public void setCharAt(int index,char ch)
The function of this method is to change the character whose index value is the index position in the object into a new character ch. For example:
1
2
string buffer sb = new string buffer(" ABC ");
sb.setCharAt( 1,' D ');
The value of the object sb becomes "aDc".
F.trimToSize method
1
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 length, thus reducing the waste of space.
In short, in actual use, String and StringBuffer have their own advantages and disadvantages, and you can choose the corresponding type to use according to the specific use environment.