The difference between string and StringBuilder

Source: Internet
Author: User

When we are beginners to use C #, often do not know whether to use StringBuilder appropriate or string efficient, below is my study of StringBuilder and string of the difference between the summary, share to everyone.

The string class has an immutable nature. Each time a character operation is performed, a new string object is created.

The StringBuilder class solves the problem of creating a large number of objects during repeated modifications to the string. After initializing a StringBuilder, it automatically requests a default StringBuilder capacity (the default is 16), which is controlled by capacity. And allow, we control the size of the capacity as needed, You can also get or set the length of the StringBuilder by length.

Example: Use the String class to write this

string begin_query =  "SELECT UPPER (MachineName) as MachineName,"  +< span class= "str" > "LOWER (Machineowner) as Machineowner, Status,"  +  "StartTime from net_stress WHERE "; String end_query =  "and StartTime > '"  + StartTime +  "' and StartTime < ' " + endTime + ; String query = begin_query + getwhereclause ( "PASSED" ) + end_query; 

This is written in the StringBuilder class:

stringbuilder begin_query = new  StringBuilder (); Begin_query.  Append ( "SELECT UPPER (machinename) as MachineName" ); Begin_query.  Append ( "LOWER (Machineowner) as Machineowner, Status," ); Begin_query.  Append ( "StartTime from net_stress WHERE" );  StringBuilder end_query = new  StringBuilder (); End_query.  Append ( "and StartTime >" ); End_query.  Append (StartTime); End_query.  Append ( "' and StartTime < '" ); End_query.  Append (EndTime); End_query.  Append ( "'" ); String query = Begin_query. Append (Getwhereclause ( "PASSED" )). Append (End_query). ToString (); 

By initializing a variable 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!");

Set capacity and length
Although the StringBuilder object is a dynamic object, it allows you to expand the number of characters in the string it encapsulates, but 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 length of the string that the current StringBuilder object holds. For example, you can create a new instance of the StringBuilder class with the string "Hello" (length 5), and you can specify that the object has a maximum capacity of 25. When StringBuilder is modified, it does not redistribute space for itself until the capacity is reached. When the capacity is reached, new space is automatically allocated and the capacity doubles. 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.

StringBuilder   Mystringbuilder   =   new   StringBuilder ("Hello   world!",   25 ); In addition, you can use the read/write capacity property to set the maximum length of an object. The following code example uses the capacity property to define the maximum length of an object.
Mystringbuilder.capacity   =   25;

The

ensurecapacity   method can be used to check the capacity of the current    stringbuilder   . If the capacity is greater than the value passed, no changes are made, but if the capacity is less than the value passed, the current capacity is changed so that it matches the value passed.   can also view or set    length   properties. If you set the    length   property to a value that is greater than the    capacity   property, the     capacity is automatically The    property changes to the same value as the    length   property. If you set the    length   property to a value that is less than the length of the string within the current     stringbuilder   object, the string is shortened.  
Modify    stringbuilder    string  


The following table lists the methods that you can use to modify the content of    stringbuilder  .   
stringbuilder.append   Appends the information to the end of the current    stringbuilder  .     
stringbuilder.appendformat   The format specifier passed in the string with formatted text substitution.    
stringbuilder.insert   Inserts a string or an object into the current    stringbuilder   Object at the specified index.    
stringbuilder.remove   Removes the specified number of characters from the current    stringbuilder   object.    
stringbuilder.replace   replaces the specified character at the specified index. The    
Append  
append   method can be used to add a string representation of text or an object to the current    The stringbuilder    object represents the end of the string. The following example initializes a    stringbuilder   object to "hello    world" and appends some text to the end of the object. The space is automatically allocated as needed.

StringBuilder   Mystringbuilder   =   new   StringBuilder ("Hello   world!");   Mystringbuilder.append ("   what   a   beautiful day   .");   

AppendFormat
The AppendFormat method adds text to the end of StringBuilder and implements the IFormattable interface, so the standard format string described in the formatting section can be accepted. You can use this method to customize the format of a variable and append these values to the back of the StringBuilder. The following example uses the AppendFormat method to place an integer value formatted as a currency value to the end of StringBuilder.

int   MyInt   = +   ;    StringBuilder   Mystringbuilder   =   new   StringBuilder ("Your   Total is ");  Mystringbuilder.appendformat ("{0:c}   ",   MyInt);  

Insert
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.

StringBuilder   Mystringbuilder   =   new   StringBuilder ("Hello   world!");  Mystringbuilder.insert (6,"Beautiful   ");  

Remove
You can use the Remove method to remove a specified number of characters from the current StringBuilder, and the removal process starts at the specified zero-based index. The following example uses the Remove method to shorten the StringBuilder.

StringBuilder   Mystringbuilder   =   new   StringBuilder ("Hello   world!");  Mystringbuilder.remove (5,7);  

Replace
Using the Replace method, you can replace characters within a StringBuilder object with another specified character.   The following example uses the Replace method to search for a StringBuilder object, find all the exclamation-point characters (!), and use the question mark character (?). To replace them.

StringBuilder   Mystringbuilder   =   new   StringBuilder ("Hello   world!");  Mystringbuilder.replace ('! ',   '? ');  Console.WriteLine (Mystringbuilder);  

Difference between string and StringBuilder (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.