Common methods of Java StringBuffer
StringBuffer is a variable length string
Example 1: Additional deletion insert reversal
append addition
Delete delete
Insert insert
reverse reversal
package character; public class TestString { public static void main(String[] args) { String str1 = "let there "; StringBuffer sb = new StringBuffer(str1); //Create a StringBuffer object based on str1 sb.append("be light"); //In the final addition System.out.println(sb); sb.delete(4, 10);//Delete characters between 4 and 10 System.out.println(sb); sb.insert(4, "there ");//Insert there at 4 locations System.out.println(sb); sb.reverse(); //Reversal System.out.println(sb); } }
Example 2: Length capacity
Why can StringBuffer grow longer?
Like a character array inside String, StringBuffer maintains an array of characters. However, this character array has redundant length
For example, the new StringBuffer("the") has an internal character array of 19 instead of 3, so that the call insertion and addition can be completed on the basis of the existing array.
If the additional length exceeds 19, a new array will be allocated, which is a little longer than the original one. If the original data is copied into the new array, it seems that the length of the array will be longer.
length: length of "the" 3
capacity: total allocated space 19
Note: The number of JDK varies with the number of JDK.
package character; public class TestString { public static void main(String[] args) { String str1 = "the"; StringBuffer sb = new StringBuffer(str1); System.out.println(sb.length()); //Content Length System.out.println(sb.capacity());//Total space } }
Practice: StringBuffer performance
Generate 10-bit random strings
Then, we use String's +, connect 10,000 random strings, and calculate the time consumed.
Then, we use StringBuffer to connect 10,000 random strings to calculate the time consumed.
Tip: Use System.currentTimeMillis() to get the current time (milliseconds)
Answer:
package character; public class TestString { public static void main(String[] args) { int total = 10000; String s = randomString(10); StringBuffer sb = new StringBuffer(); String str1 = ""; long start = System.currentTimeMillis(); for (int i = 0; i <total; i++) { str1+=s; } long end = System.currentTimeMillis(); System.out.printf("Using string connection+The way, the connection%d Time-consuming%d Millisecond%n",total,end-start); total *=100; start = System.currentTimeMillis(); for (int i = 0; i <total; i++) { sb.append(s); } end = System.currentTimeMillis(); System.out.printf("Use StringBuffer The way, the connection%d Time-consuming%d Millisecond%n",total,end-start); } private static String randomString(int length) { String pool = ""; for (short i = '0'; i <= '9'; i++) { pool += (char) i; } for (short i = 'a'; i <= 'z'; i++) { pool += (char) i; } for (short i = 'A'; i <= 'Z'; i++) { pool += (char) i; } char cs[] = new char[length]; for (int i = 0; i < cs.length; i++) { int index = (int) (Math.random() * pool.length()); cs[i] = pool.charAt(index); } String result = new String(cs); return result; } }