String objects are unchangeable. Every time you use one of the methods in the System.String class, you need to create a new string object in memory, which requires allocating new space for the new object. The system overhead associated with creating a new String object can be very expensive in situations where repeated modifications are required to be performed on the string. If you want to modify a string without creating a new object, you can use the System.Text.StringBuilder class. For example, using the StringBuilder class can improve performance when concatenating many strings together in a loop.
By initializing the variable with an overloaded constructor method, a new instance of the StringBuilder class can be created, as explained in the following example.
StringBuilder MyStringBuilder = new StringBuilder("Hello World!");(I) Set capacity and length
Although the StringBuilder object is a dynamic object that allows 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 that the current StringBuilder object holds. For example, you can create a new instance of the StringBuilder class with the string "Hello" (length 5), while specifying that the maximum capacity of the object is 25. When a StringBuilder is modified, it does not reallocate space for itself until the capacity is reached. When capacity is reached, new space will be automatically allocated and the capacity will be doubled. You can use one of the overloaded constructors to specify the capacity of the StringBuilder class. The following code example specifies that the MyStringBuilder object can be expanded to a maximum of 25 blanks.
StringBuilderMyStringBuilder = new StringBuilder("Hello World!", 25); Additionally, the maximum length of the object can be set using the read/write Capacity property. The following code example uses the Capacity property to define the maximum length of an object.
MyStringBuilder.Capacity= 25;
(II) The following lists several common methods of this type:
(1) The Append method can be used to add a string representation of a text or object to the end of a string represented by the current StringBuilder object. The following example initializes a StringBuilder object to "Hello World" and then appends some text to the end of the object. Space will be automatically allocated as needed.
StringBuilderMyStringBuilder = new StringBuilder("Hello World!");MyStringBuilder.Append(" What a beautiful day.");Console.WriteLine(MyStringBuilder);This example displays Hello World! What abeautiful day. to the console.
(2) The AppendFormat method adds text to the end of StringBuilder and implements the IFormattable interface, so it can accept the standard format strings described in the formatting section. You can use this method to customize the format of the variable and append these values to the back of the StringBuilder. The following example uses the AppendFormat method to place an integer value set to currency value format to the end of the 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 position in the current StringBuilder. The following example uses this method to insert a word into the sixth position of the StringBuilder.
StringBuilderMyStringBuilder = new StringBuilder("Hello World!");MyStringBuilder.Insert(6,"Beautiful");Console.WriteLine(MyStringBuilder);This example displays Hello BeautifulWorld! to the console.
(4) You can use the Remove method to remove a specified number of characters from the current StringBuilder, and the removal process starts from the specified index starting from zero. The following example uses the Remove method to shorten the StringBuilder.
StringBuilderMyStringBuilder = new StringBuilder("Hello World!");MyStringBuilder.Remove(5,7);Console.WriteLine(MyStringBuilder);This example displays Hello to the console.
(5) Using the Replace method, you can replace the characters in the StringBuilder object with another specified character. The following example uses the Replace method to search for StringBuilder objects, find all exclamation mark characters (!), and replace them with question mark characters (?).
StringBuilderMyStringBuilder = new StringBuilder("Hello World!");MyStringBuilder.Replace('!', '?');Console.WriteLine(MyStringBuilder);This example displays Hello World? to the console
getSqlMapClientTemplate().queryForList((new StringBuilder()).append(entityClass.getName()).append(".select").toString(), null);Java's StringBuilder class
If the program has a frequent need for additional strings, it is not recommended to use + for concatenation of strings. You can consider using the java.lang.StringBuilder class. The objects 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 attached characters exceed the accommodateable length, the StringBuilder object automatically increases the length to accommodate the attached characters. If there is a need to frequently attach strings, using the StringBuilder class can greatly improve 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 (""); 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)); }}This code outputs:
Execution time: 3188
Execution time: 15
StringBuilder is a new class that was added only to j2se1.5.0. If the previous version had the same requirements, java.util.StringBuffer will be used. In fact, StringBuilder is designed to have the same operating interface as StringBuffer. Using StringBuilder in a stand-alone non-thread situation will have better efficiency because StringBuilder does not handle synchronization issues. StringBuffer will handle synchronization problems. If StringBuilder will be operated under multiple threads, you should use StringBuffer instead to let the object manage synchronization problems by itself.
The above brief analysis of the usage of stringBuilder in Java is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.