[FAQ] What are the differences between String (String phase) and StringBuilder, principles, and weaknesses?

Source: Internet
Author: User

Original in http://www.dotblogs.com.tw/mis2000lab/archive/2013/09/09/msdn_string_stringbuilder.aspx

[FAQ] What are the differences between String (String phase) and StringBuilder, principles, and weaknesses?

 

 

This is a FAQ.

Today, I saw an example on the msdn website, which is very clear.

 

In addition to making a record for yourself, you will be able to use it in the future.

I also share with you.

 

Source:Http://msdn.microsoft.com/zh-tw/library/system.string.aspx

========================================================== ========================================================== ==========

Immutability andStringBuilderCategory

 

The String object is calledUnchangeable(Read-Only)BecauseAfter it is createdConvenient"No"Modify its value.

It seems that the method of modifying the String object will actually return the modified"New"String object.

 

Because the string isUnchanged(Immutable), Executing the string processing routine to repeat or delete a single string may significantly affect the performance.

For example, the following program code uses a random number generator to create a string containing 0001 characters in the range from 0x1000 to 0x052F.

 

Although the program code seems to be usedString concatenation (C #Use+Symbol.VBUse&Symbol)Append the new character to an existing string named str,

But itIt will actually create"New"StringObject.

 

Using System;

Using System. IO;

Using System. Text;

 

Public class Example

{

Public static void Main ()

{

Random rnd = new Random ();

 

String str = String. Empty;

StreamWriter sw = new StreamWriter (@ ". \ StringFile.txt", false, Encoding. Unicode );

 

For (int ctr = 0; ctr <= 1000; ctr ++ ){

Str+ =Convert. ToChar (rnd. Next (1, 0x0530 ));

If (str. Length % 60 = 0)

Str+ =Environment. NewLine;

}

Sw. Write (str );

Sw. Close ();

}

}

 

 

Imports System. IO

Imports System. Text

 

Module Example

Public Sub Main ()

Dim rnd As New Random ()

 

Dim str As String = String. Empty

Dim sw As New StreamWriter (". \ StringFile.txt", False, Encoding. Unicode)

 

For ctr As Integer = 0 To 1000.

Str& =ChrW (rnd. Next (1, & h0530 ))

If str. Length Mod 60 = 0 Then str& =VbCrLf

Next

Sw. Write (str)

Sw. Close ()

End Sub

End Module

 

========================================================== ========================================================== ==========

 

InPerform"Multiple changes"You can useStringBuilderCategoryReplaces the String category.

 

Unlike the String class execution entity, the StringBuilder object isVariable;

 

When you concatenate, attach, or delete a substring in a string, the job isSingle string.

 

When you modify the value of the StringBuilder object, you can call its StringBuilder. ToString ()MethodTo convert it to a string.

 

The following example will replace the String used for concatenating any character between 1000 to 0x0001 to 0x052F with the StringBuilder object.

 

Using System;

Using System. IO;

UsingSystem. Text;// For StringBuilder

 

Public class Example

{

Public static void Main ()

{

Random rnd = new Random ();

StringBuilder sb =New StringBuilder ();

StreamWriter sw = new StreamWriter (@ ". \ StringFile.txt", false, Encoding. Unicode );

 

For (int ctr = 0; ctr <= 1000; ctr ++ ){

Sb. Append(Convert. ToChar (rnd. Next (1, 0x0530 )));

If (sb. Length % 60 = 0)

Sb. AppendLine ();

}

Sw. Write (sb. ToString ());

Sw. Close ();

}

}

 

 

 

Imports System. IO

ImportsSystem. Text'-- For StringBuilder

 

Module Example

Public Sub Main ()

Dim rnd As New Random ()

Dim sbNew StringBuilder ()

Dim sw As New StreamWriter (". \ StringFile.txt", False, Encoding. Unicode)

 

For ctr As Integer = 0 To 1000.

Sb. Append(ChrW (rnd. Next (1, & h0530 )))

If sb. Length Mod 60 = 0 Then sb. AppendLine ()

Next

Sw. Write (sb. ToString ())

Sw. Close ()

End Sub

End Module

 

 

 

 

Previously tested, string-connected, StringBuilderExecution speed, Please read this article --

Http://www.dotblogs.com.tw/mis2000lab/archive/2009/11/25/vs2010_vs2008_who_fast.aspx

 

 

1.Large text count

 

2.Extracted from the data table field and the string lengthTransient... StringBuilder has an absolute advantage.

 

 

 

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.