Characteristics:
Length is changeable (bottom level is actually constant new array)
You can manipulate multiple data types directly
Will eventually become a string through the ToString method
You can modify a string
The thread is synchronized
The difference from the array:
StringBuffer: Length is variable and can store different types of data
Array: The length is immutable, only one data type can be stored
StringBuffer function:
Storage (Create)
StringBuffer append (): Adds the specified data as a parameter to the end of the existing data (except for the byte and short types of data that are available for any other data type)
StringBuffer Insert (Index, data): You can insert data into the specified index position
Copy Code code as follows:
public static void Method_add () {
StringBuffer sb = new StringBuffer ();
Sb.append ("abc"). Append (True). Append (5378); method call chain result is a string of strings
StringBuffer sb1 = Sb.append (34563);
System.out.println ("SB==SB1:" + (SB==SB1))//The result is true SB and SB1 point to the same object, which is called the basin theory, no matter what result is added to the StringBuffer container or the container
add element at specified location
StringBuffer SB2=SB1. Insert (3, "Hello");
SOP (Sb2.tostring ());
}
Remove (delete)
StringBuffer Delete (start,end) deletes the data in the buffer, contains start, does not contain end, and throws an exception string corner if start exceeds the corner mark
StringBuffer Deletecharat (index) deletes characters at the specified location
Copy Code code as follows:
public static void Method_del () {
StringBuffer sb = new StringBuffer ("Huangjianfeng");
System.out.println (Sb.tostring ());
Delete a part
Sb.delete (1,3);
System.out.println (Sb.tostring ());
Delete one of the elements in a string
Sb.delete (2,3);
Sb.deletecharat (2);
System.out.println (Sb.tostring ());
Empty buffer
Sb=new stringbuffer//error, which is also defined by a buffer SB points to a new buffer
Sb.delete (0,sb.length ());
System.out.println (Sb.tostring ());
}
Get (Read)
Copy Code code as follows:
char charAt (int index)
int indexOf (String str)
int LastIndexOf (String str)
int Length ()
String substring (int start, int end)
Copy Code code as follows:
public static void Method_read () {
StringBuffer sb = new StringBuffer ("JavaScript");
char ch = sb.charat (3);
int index1 = Sb.indexof ("SC");
int index2 = Sb.lastindexof ("SC");
int len = Sb.length ();
String str = sb.substring (5,7);
SOP ("Ch=", "+ch+", index1= "+index1+", index2= "+index2+", len= "+len+", str= "+str");
}
Modify (Update)
Copy Code code as follows:
StringBuffer replace (start,end,string);
void Setcharat (int index,char ch); Only one character can be replaced
Copy Code code as follows:
public static void Method_update () {
StringBuffer sb1 = new StringBuffer ("JavaScript");
StringBuffer SB2 = Sb1.replace (4,10, "ee");
System.out.println (SB2);
Sb1.setcharat (1, ' B ');
System.out.println (SB1);
}
Other common methods:
Reverse StringBuffer reverse ();
Set the length of the StringBuffer container SetLength ();
Stores the specified data in the buffer to the specified character array void getChars (int srcbegin,int srcend,char[] dst,int dstbegin);
Copy Code code as follows:
public static void Method_other () {
StringBuffer sb = new StringBuffer ();
Set length
Sb.setlength (4);
System.out.println (Sb.length ());
Invert a string
StringBuffer sb1 = new StringBuffer ("Huangjianfeng");
SB1 = Sb1.reverse ();
System.out.println (SB1);
Stores the specified data in the buffer to the specified character array
char[] arr = new CHAR[15];
StringBuffer SB2 = new StringBuffer ("Huangjianfeng");
Sb2.getchars (0, 7, arr, 3);
for (char Sbs:arr) {
System.out.print (SBS);
}
}
After the JDK1.5 version appeared StringBuilder
StringBuilder are threads that are not synchronized and are commonly used for single-threaded Chengti high efficiency stringbuffer are thread-synchronized, often used for multi-threaded development recommendations for using StringBuilder, to enhance efficiency
Upgrading three factors: improving efficiency, simplifying writing and improving security