String and StringBuilder- use StringBuilder class (MSDN)

xiaoxiao2021-03-06  90

String objects are not changeable. Every time you use the method in the System.String class, you must create a new string object in memory, which requires a new space for the new object. In the case where you need to repeat the string, the system overhead associated with creating new String objects may be very expensive. If you want to modify the string without creating a new object, you can use it.

System.Text.StringBuilder

class. For example, when a multi-string is connected together, use the StringBuilder class to improve performance.

By initializing the variables with a heavy-duty constructor, a new instance of the StringBuilder class can be created, as explained in the following examples.

[Visual Basic]

Dim MyStringBuilder As New StringBuilder ("Hello World!")

[C #]

Stringbuilder MyStringBuilder = New StringBuilder ("Hello World!");

Set capacity and length

Although the StringBuilder object is a dynamic object, it allows the number of characters in the string it encapsulated, but you can specify a value for its maximum number of characters it can accommodate. This value is called the capacity of the object, and it should not be confused with the string length of the current StringBuilder object. For example, you can create a new instance of the string "Hello" (length 5) of the StringBuilder class, while specifying the maximum capacity of the object is 25. When you modify the StringBuilder, it does not reassign the space yourself before the capacity is reached. When the capacity is reached, the new space is automatically allocated and the capacity is doubled. You can use one of the overloaded constructor to specify the capacity of the StringBuilder class. The following code example specifies to expand the MyStringBuilder object to a maximum of 25 blank.

[Visual Basic]

DIM MyStringBuilder As New StringBuilder ("Hello World!", 25)

[C #]

Stringbuilder MyStringBuilder = New StringBuilder ("Hello World!", 25);

In addition, you can use read / write

Capacity

Attributes to set the maximum length of the object. The following code example uses the Capacity property to define the maximum length of the object.

[Visual Basic]

MyStringBuilder.capacity = 25

[C #]

MyStringBuilder.capacity = 25;

EnsureCapacity

Methods can be used to check the capacity of the current StringBuilder. If the capacity is greater than the value passed, no change is made; however, if the capacity is less than the value passed, the current capacity is changed to match the passing value.

You can also view or set up

Length

Attributes. If you set the Length property to a value greater than the Capacity property, the Capacity property is automatically changed to the same value as the LENGTH property. If the Length property is set to a value that is less than the string length within the current StringBuilder object, the string is shortened.

Modify StringBuilder string

The following table lists methods that can be used to modify the content of StringBuilder.

Method name

StringBuilder.Append

Add information to the end of the current StringBuilder.

Stringbuilder.AppendFormat

Replace the format specifier passed in the string with format text.

Stringbuilder.insert

Insert strings or objects to the specified index of the current StringBuilder object. Stringbuilder.remove

Remove the specified number of characters from the current StringBuilder object.

Stringbuilder.replace

Replace the specified character at the specified index.

Append

The APPEND method can be used to add a string representation of text or objects to the end of the string represented by the current StringBuilder object. The following example initials a StringBuilder object to "Hello World" and then append some text to the end of the object. The space will be automatically allocated as needed.

[Visual Basic]

Dim MyStringBuilder As New StringBuilder ("Hello World!")

MyStringBuilder.Append ("what a beautiful day.")

Console.writeline (MyStringBuilder)

[C #]

Stringbuilder MyStringBuilder = New StringBuilder ("Hello World!");

MyStringBuilder.Append ("What a beautiful day.");

Console.writeLine (MyStringBuilder);

This example will Hello World! What a beautiful day. Displayed to the console.

Appendformat

The AppendFormat method adds text to the end of the StringBuilder, and implements the IFORMATTABLE interface, so that the standard format string described in the format section can be accepted. You can use this method from defining 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 of the currency value format to the end of the StringBuilder.

[Visual Basic]

DIM Myint as integer = 25

DIM MyStringBuilder As New StringBuilder ("Your Total IS")

MyStringBuilder.AppendFormat ("{0: C}", Myint)

Console.writeline (MyStringBuilder)

[C #]

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.

Insert

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 location of the StringBuilder.

[Visual Basic]

Dim MyStringBuilder As New StringBuilder ("Hello World!")

MyStringBuilder.Insert (6, "beautiful")

Console.writeline (MyStringBuilder)

[C #]

Stringbuilder MyStringBuilder = New StringBuilder ("Hello World!");

MyStringBuilder.Insert (6, "beautiful");

Console.writeLine (MyStringBuilder; this example will Hello Beautiful World! Show to the console.

Remove

You can use the REMOVE method to remove the specified number of characters from the current StringBuilder, and the removal process starts from the specified index from the zero. The following example whose StringBuilder is shortened using the Remove method.

[Visual Basic]

Dim MyStringBuilder As New StringBuilder ("Hello World!")

MyStringBuilder.Remove (5, 7)

Console.writeline (MyStringBuilder)

[C #]

Stringbuilder MyStringBuilder = New StringBuilder ("Hello World!");

MyStringBuilder.Remove (5, 7);

Console.writeLine (MyStringBuilder);

This example displays Hello to the console.

Replace

With the Replace method, you can replace the characters within the StringBuilder object with another specified character. The following example uses the Replace method to search for the StringBuilder object, find all the exclamation characters (!), And replace them with the question mark character (?).

[Visual Basic]

Dim MyStringBuilder As New StringBuilder ("Hello World!")

MyStringBuilder.Replace ("!" C, "? C)

Console.writeline (MyStringBuilder)

[C #]

Stringbuilder MyStringBuilder = New StringBuilder ("Hello World!");

MyStringBuilder.Replace ('!', '?');

Console.writeLine (MyStringBuilder);

This example will Hello World® to the console.

转载请注明原文地址:https://www.9cbs.com/read-96653.html

New Post(0)