JAVA course 31st (Common Object API)-StringBuffer Class & amp; StringBuilder class
StringBuffer class
The principle of string composition is implemented through this class
StringBuffer can add or delete string content
StringBuffer is a container
Many methods are the same as strings.
I. Features
StringBuffer string buffer, used to store data in containers
1. Variable Length
2. Different data types can be stored
3. convert it to a string for use.
4. Modify the string
Container features:
1. Add
Append (data)
Insert (index, data );
2. Delete
StringBuffer delete (int start, int end) // contains the header, not the end
deleteCharAt(int index)
Removechar
. //
3. Search
Char charAt (index)
Int indexOf (string)
Int lastindexOf (string)
4. Modify
StringBuffer replace (start, end, string)
Void setcharAt (intdex, char)
CURD (add, delete, modify, and query)
5. Others
Setlength (data)
Reseve reverse the string
Public class Main {public static void main (String [] args) {// StringBufferDemo (); // Add // StringBufferDemo_1 (); StringBufferDemo_2 (); // Delete StringBufferDemo_3 (); // modify} public static void StringBufferDemo () {StringBuffer SB = new StringBuffer (); StringBuffer S1 = SB. append (4); System. out. println (SB); System. out. println (S1); System. out. println (SB = S1); S1 = SB. append (true ). append ("nihao"); System. out. println (SB); SB. insert (1, "hehe"); // Add "hehe" System at location 1. out. println (SB);} public static void StringBufferDemo_1 () {StringBuffer SB = new StringBuffer ("qwer"); SB. insert (2, 5); // qw5erSystem. out. println (SB);} public static void StringBufferDemo_2 () {StringBuffer SB = new StringBuffer ("qwer"); SB. deleteCharAt (2); // delete an SB. delete (1, 3); // clear SB. delete (0, SB. length (); System. out. println (SB);} public static void StringBufferDemo_3 () {StringBuffer SB = new StringBuffer ("qwert"); SB. replace (0, 3, "aaaaaaa"); System. out. println (SB); SB. setCharAt (2, 'G'); // note that it is voidSB. setLength (2); System. out. println ("SB =" + SB); // if the length of the Set exceeds the length of the string itself, fill the System with space in the excess part. out. println ("LEN =" + SB. length (); SB. reverse (); // reverse the string System. out. println (SB );}}
Construction Method: StringBuffer ()
Construct a string buffer with no characters. The initial capacity is 16.
Variable Length Array
When data is stored in the array, a new array is automatically opened up. The length of the new book group is about twice the length of the original array, copy the array of the original array and store new data. Pay attention to efficiency issues.
Ii. StringBuilder class
It is actually the same as StringBuffer.
The major difference is that the birth time is different.
StringBuffer: ensure synchronization and thread safety (JDK1.0): usually used for Multithreading
StringBuilder: not guaranteed synchronization (JDK1.5): usually used in a single thread. It appears to improve efficiency.
StringBuilder should be used first. It is highly efficient because it does not guarantee synchronization.
class StringBuffer{Object obj = new Object();public StringBuffer append() {synchronized (obj) {}}public StringBuffer delete() {synchronized (obj) {}}.....}
The lock is added once, so the efficiency is low.
Iii. exercises:
Public class Main {public static void main (String [] args) {int [] arr = {1, 2, 4, 5, 6, 7, 8, 9}; String str = ArryToString (arr ); system. out. println ("str:" + str);}/* convert the int array to a String */public static String ArryToString (int [] arr) {// String str = "["; stringBuilder str = new StringBuilder (); str. append ("["); for (int I = 0; I <arr. length-1; I ++) {// str + = arr [I] + ","; // once the String constant pool does not have ",", it must be generated in the String constant pool, so use StringBuilderstr whenever possible. append (arr [I] + ",") ;}// str + = arr [arr. length-1] + "]"; str. append (arr [arr. length-1] + "]"); return str. toString ();}}