1. Initialization of StringBuffer objects
The initialization of the StringBuffer object is not the same as the initialization of the string class, Java provides special syntax, and typically is initialized with a constructor method.
For example:
StringBuffer s = new StringBuffer ();
This initializes the StringBuffer object as an empty object, which is the error I made.
If you need to create a StringBuffer object with content, you can use:
StringBuffer s = new StringBuffer ("abc");
The content of this initialized StringBuffer object is the string "abc".
It is important to note that StringBuffer and string are of different types and cannot be cast directly, and 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
The code for the StringBuffer between the object and the string object is as follows:
String s = "abc";
StringBuffer sb1 = new StringBuffer ("123");
StringBuffer SB2 = new StringBuffer (s); Convert String to StringBuffer
String S1 = sb1.tostring (); StringBuffer Convert to String
2. Common methods of StringBuffer
The methods in the StringBuffer class mainly focus on changes to strings, such as Append, insert, and delete, which are the main differences between the StringBuffer and the string classes.
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.
Note: The above content is quoted from http://blog.csdn.net/jason0539;
The use of StringBuffer