Preface
This article introduces a java string processing technique-using StringBuilder to improve the string processing efficiency.
StringBuilder
When the program needs to splice strings frequently, the efficiency of using strings directly is relatively low.
The internal implementation of StringBuilder optimizes the processing of concatenation operations. Therefore, when you need to splice a String frequently, this class should be used first instead of the String class.
Procedure
1. Define the StringBuilder object
2. Concatenate strings
3. Convert the StringBuilder object back to the String object
Sample code
1 package test; 2 3 // demonstrate the standard String concatenation method 4 public class Test {5 6 public static void main (String [] args) {7 8 // Step 1: construct a StringBuilder object 9 StringBuilder builder = new StringBuilder (); 10 11 // Step 2: splice the String 12 String str = "test String" to the object; 13 builder. append (str); 14 15 // Step 3: convert the StringBuilder object to the String object 16 str = builder. toString (); 17 18 // Display result 19 System. out. print (str); 20} 21}
Running result
Summary
1. This class is specially used to process string concatenation.
1. If thread security support is required, use StringBuffer.
Use StringBuilder to process strings more efficiently