Application method and example analysis of Java StringBuilder
String objects are immutable. Every time you use system One of the methods in the string class needs to create a new string object in memory, which requires allocating new space for the new object. The overhead associated with creating a new string object can be very expensive in cases where repeated modifications to the string are required. If you want to modify the string without creating a new object, you can use system Text. StringBuilder class. For example, using the StringBuilder class can improve performance when many strings are connected together in a loop.
By initializing variables with an overloaded constructor method, you can create a new instance of the StringBuilder class, as illustrated in the following example.
StringBuilder MyStringBuilder = new StringBuilder("Hello World!");
(1) Set capacity and length although the StringBuilder object is a dynamic object that allows you to expand the number of characters in the string it encapsulates, you can specify a value for the maximum number of characters it can hold. This value is called the capacity of the object and should not be confused with the string length held by the current StringBuilder object. For example, You can create a StringBuilder class with the string "hello" (length 5), and the maximum capacity of the object can be specified as 25. When modifying StringBuilder, it will not reallocate space for itself until the capacity is reached. When the capacity is reached, new space will be automatically allocated and the capacity will be doubled. The capacity of StringBuilder class can be specified by using one of the overloaded constructors. The following code example refers to The mystringbuilder object can be expanded to a maximum of 25 blanks. StringBuilderMyStringBuilder = new StringBuilder("Hello World!", 25); In addition, you can use the read / write capacity property to set the maximum length of the object. The following code example uses the capacity attribute to define the maximum length of an object. MyStringBuilder. Capacity= 25;
(2) Several common methods of this class are listed below: (1) the append method can be used to add the string representation of text or object to the end of the string represented by the current StringBuilder object. The following example initializes a StringBuilder object to "Hello world" and appends some text to the end of the object. Space will be allocated automatically as needed. StringBuilderMyStringBuilder = new StringBuilder("Hello World!"); MyStringBuilder. Append(" What a beautiful day."); Console. WriteLine(MyStringBuilder); This example will Hello world! What abeautiful day. Display to console.
(2) The appendformat method adds text to the end of StringBuilder and implements the IFormattable interface, so it can accept the standard format string described in the formatting section. You can use this method to customize the format of variables and append these values to StringBuilder. The following example uses the appendformat method to place an integer value formatted as a monetary value at the end of StringBuilder. int MyInt= 25; StringBuilder MyStringBuilder = new StringBuilder("Your total is "); MyStringBuilder. AppendFormat("{0:C} ",MyInt); Console. WriteLine(MyStringBuilder); This example displays your total is $25.00 to the console.
(3) The insert method adds a string or object to the specified location in the current StringBuilder. The following example uses this method to insert a word into the sixth position of StringBuilder. StringBuilderMyStringBuilder = new StringBuilder("Hello World!"); MyStringBuilder. Insert(6,"Beautiful "); Console. WriteLine(MyStringBuilder); This example will Hello beautifulworld! Display to console.
(4) You can use the delete method to remove a specified number of characters from the current StringBuilder, starting at the specified zero based index. The following example uses the remove method to shorten StringBuilder. StringBuilderMyStringBuilder = new StringBuilder("helloooo"); MyStringBuilder. delete(5,7); Console. WriteLine(MyStringBuilder); This example displays helloo to the console.
(5) Using the replace method, you can replace a character in a StringBuilder object with another specified character. The following example uses the replace method to search the StringBuilder object for all exclamation point characters (!), And use the question mark character (?) To replace them. StringBuilderMyStringBuilder = new StringBuilder("Hello World!"); MyStringBuilder. Replace('!','?'); Console. WriteLine(MyStringBuilder);
This example will be Hello world? Display to console
getsqlMapClientTemplate(). queryForList((new StringBuilder()). append(entityClass.getName()). append(".select"). toString(),null);
StringBuilder class of Java
If the program needs additional strings frequently, it is not recommended to use + to concatenate strings. Consider using Java Lang. StringBuilder class. The object generated by using this class will have a length of 16 characters by default. You can also specify the initial length by yourself. If the additional characters exceed the allowable length, the StringBuilder object automatically increases the length to accommodate the attached characters. If there is a need to attach strings frequently, the use of StringBuilder class can greatly improve the efficiency. The following code: Java code public class appendstringtest
{ public static void main(String[] args) { String text = "" ;
long beginTime = System. currentTimeMillis(); for ( int i= 0 ;i< 10000 ;i++) text = text + i; long endTime = System. currentTimeMillis(); System. out. Println ("execution time:" + (Endtime begintime));
StringBuilder sb = new StringBuilder ( "" ); beginTime = System. currentTimeMillis(); for ( int i= 0 ;i< 10000 ;i++) sb. append(String.valueOf(i)); endTime = System. currentTimeMillis(); System. out. Println ("execution time:" + (Endtime begintime));
} } public class AppendStringTest { public static void main(String[] args) {
String text = ""; long beginTime = System. currentTimeMillis(); for(int i=0;i<10000;i++) text = text + i; long endTime = System. currentTimeMillis(); System. out. Println ("execution time:" + (Endtime begintime)); StringBuilder sb = new StringBuilder ("China"); beginTime = System. currentTimeMillis(); for(int i=0;i<1;i++) sb. Append ("\" Taiwan \ ""); endTime = System. currentTimeMillis(); System. out. Println ("execution time:" + (Endtime begintime));
}} output of this Code: China "Taiwan"
Execution time: 3188 execution time: 15
StringBuilder is j2se1 5.0. If the previous versions have the same requirements, use Java util. StringBuffer。 In fact, StringBuilder is designed to have the same operation interface as StringBuffer. Using StringBuilder in the case of single machine non thread will be more efficient, because StringBuilder does not deal with the problem of synchronization. StringBuffer will handle synchronization problems. If StringBuilder will be operated under multiple threads, use StringBuffer instead to let the object manage synchronization problems by itself.
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.