String Processing Summary II (C#stringbuilder Class)

Source: Internet
Author: User

Dynamic string StringBuilder

The System.Text.StringBuilder class can implement dynamic strings compared to the string class. In addition, the dynamic meaning is that when the string is modified, the system does not need to create a new object, does not repeatedly open up new memory space, but directly on the original StringBuilder object based on the modification. Below, the StringBuilder class is discussed in detail from the perspective of each application.

1. Declaring the StringBuilder string

The StringBuilder class is located in the namespace System.Text, and when used, the space can be introduced by using a statement in the file header:

Using System.Text;

Declaring a StringBuilder object requires the use of the new keyword, and it can be initialized. The following statement declares a StringBuilder object Mystringbuilder and initializes it as "Hello":

Stringbuildermystringbuilder=new StringBuilder ("Hello");

If you do not use the Using keyword to introduce the System.Text namespace in the file header, you can also declare the StringBuilder object through spatial qualification:

System.text.stringbuildermystringbuilder=new StringBuilder ("Hello");

At the time of declaration, it is also possible to give no initial value and then assign the value by its method.

2. Set StringBuilder capacity

The StringBuilder object is a dynamic string that can be extended by the number of characters it sets. Alternatively, you can set a maximum length, which is called the StringBuilder object's capacity (capacity).

The significance of setting the capacity for StringBuilder is that, when modifying the StringBuilder string, StringBuilder does not reallocate space until its actual character length (that is, the number of characters already in the string) does not reach its capacity, and when the capacity is reached, The StringBuilder is automatically not set on the basis of the original space, and the StringBuilder default initial allocation is 16 characters in length. There are two ways to set the capacity of a StringBuilder object.

1. Using constructors

The StringBuilder constructor can accept capacity parameters, for example, the following declares a StringBuilder object SB2, and sets its capacity to 100.

Using constructors

StringBuilder sb2=new StringBuilder ("Hello", 100);

2. Using capacity read/write properties

The capacity property specifies the capacity of the StringBuilder object, such as the following statement, which first StringBuilder an object SB3, and then sets its capacity to 100 with the capacity property.

Using the Capacity property

StringBuilder sb3=new StringBuilder ("Hello");

SB3. capacity=100;

3. Append operation

Appending a StringBuilder means adding a new string to the end of the current StringBuilder string, which can be accomplished using append and AppendFormat.

1. Append method

The Append method implements simple append functions and is commonly used in the following forms:

Public StringBuilder Append (object value);

The parameter value can be either a string type or other data type, such as bool, Byte, int, and so on. In the following example, append a StringBuilder string "Hello" to "Hello world!".

Append

StringBuilder sb4=new StringBuilder ("Hello");

Sb4. Append ("world!");

2.AppendFormat method

The AppendFormat method can be used to format the appended part of the string, define the format of the variable, and append the formatted string to the StringBuilder. The commonly used forms are:

StringBuilder AppendFormat (String format,params object[] args);

Where the args array specifies the multiple variables to append. The format parameter contains a string of formatting specifications, including a series of format characters enclosed in curly braces, such as {0:u}. Here, 0 represents the No. 0 variable in the args parameter array, and ' U ' defines its format. In the following example, append a StringBuilder string "Today is" to "today is * current Date *\".

AppendFormat

StringBuilder sb5=new StringBuilder ("Today is");

SB5. AppendFormat ("{0:yyyy-mm-dd}", System.DateTime.Now);

Console.WriteLine (SB5); Form: "Today is 2008-10-20"

4. Insert operation

The StringBuilder insert operation refers to inserting a new string into the specified position of the current StringBuilder string, such as "Hello" into "Heeeello". You can use the Insert method of the StringBuilder class to implement this function, which is commonly used in the following ways:

Public StringBuilder Insert (int index, object value);

Where the parameter index specifies the position to be inserted, and the index starts at 0, such as Index=1, the insert is preceded by the 2nd character of the original string; As with append, the parameter value is not only desirable for string types.

5. Delete operation

The delete operation of StringBuilder can remove a certain number of characters from the current StringBuilder string, such as "Heeeello" into "Hello". You can use the Remove method of the StringBuilder class to implement this function, which is commonly used in the following ways:

Public StringBuilder Remove (int startIndex, int length);

Where the parameter startindex specifies the starting position to delete, meaning the same as the index in insert, and the length parameter specifies the number of characters to delete. In the following example, a StringBuilder string "Heeeello" is modified to "Hello" by a delete operation.

Remove

StringBuilder sb7=new StringBuilder ("Heeello");

Sb7. Remove (2,3); Delete a character after "He"

Console.WriteLine (SB7); "Hello!"

6. Replace operation

StringBuilder uses the Replace method to implement the substitution operation, such as "Hello" into "Hero", you need to replace "ll" with "R". This is very similar to the Replace method of the string class, and its common

Forms include:

Public StringBuilder Replace (Char OldChar, char Newchar);

Public StringBuilder Replace (string oldValue, string newvalue);

Where the parameters Oldchar and oldvalue are the characters and substrings to be replaced, and Newchar and NewValue are the new characters and new substrings that are replaced.

The following example replaces "Hello" with "Hero".

Replace

StringBuilder sb8=new StringBuilder ("Hello");

Sb8=sb8. Replace ("ll", "R");

Console.WriteLine (SB8); Hero

7. Compare to String

As you can see from the above, StringBuilder and string are very similar on many operations, such as INSERT, Remove, and Replace.

In terms of operational performance and memory efficiency, StringBuilder is much better than string, avoiding the creation of too many temporary string objects, especially in situations where modifications are frequently repeated. On the other hand, the string class provides more ways to enable development to implement applications faster.

On the choice of both, the StringBuilder object is recommended if the application is more stringent for system performance, memory requirements, and often handles large-scale strings, otherwise you can choose to use String.

Top
1

String Processing Summary II (C#stringbuilder Class)

Related Article

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.