C # differences between string and stringbuilder

Source: Internet
Author: User

String unchanged stringbuilder variable

C # is a common language. Here we mainly introduce C # stringbuilder and string, including stringbuilder, which is the best choice for many String concatenation or frequent operations on a string, do not use string or other features.

C #Difference between stringbuilder and string

A new instance is generated when string operations (such as value assignment and concatenation) are performed, while stringbuilder does not. Therefore, it is best to use stringbuilder when splicing a large number of strings or frequently performing operations on a string. Do not use string

In addition, we have to say a few words about stringbuilder and string:

1. It is a reference type and memory is allocated on the stack.

2. A new instance is generated during the operation.

3. Immutable)

4. Define equal operators (= and! =) Is used to compare the value of a string object (not a reference ).

C #Stringbuilder and string examples:

  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. text;
  4. Namespace example22
  5. {
  6. Class Program
  7. {
  8. Static void main (string [] ARGs)
  9. {
  10. Const intCycle=10000;
  11. LongVtickcount=Environment. Tickcount;
  12. StringStr=Null;
  13. For (intI=0; I< Cycle; I ++)
  14. STR + = I. tostring ();
  15. Console. writeline ("string: {0} msel", environment. tickcount-vtickcount );
  16. Vtickcount=Environment. Tickcount;
  17. // I am angry when I see this variable name. Why do everyone make it? :)
  18. StringbuilderSb=NewStringbuilder ();
  19. For (intI=0; I< Cycle; I ++)
  20. SB. append (I );
  21. Console. writeline ("stringbuilder: {0} msel", environment. tickcount-vtickcount );
  22. StringTmpstr1="";
  23. StringTmpstr2=Tmpstr1;
  24. Console. writeline (tmpstr1 );
  25. Console. writeline (tmpstr2 );
  26. // Note that the value of tmpstr1 does not affect the value of tmpstr2.
  27. Tmpstr1="B";
  28. Console. writeline (tmpstr1 );
  29. Console. writeline (tmpstr2 );
  30. Console. Readline ();
  31. }
  32. }
  33. }

Http://developer.51cto.com/art/200908/147430.htm

===

Is it faster to use stringbuilder to process large strings than strings?

Is it the same as the difference between stringbuffer and string in Java?

Here is an msdn help document:

Use stringbuilder
Language
C #
The string object cannot be changed. Every time you use a method in the system. string class, you must create a new String object in the memory, which requires a new space for the new object. If you need to modify the string repeatedly, the system overhead associated with creating a new String object may be very expensive. If you want to modify the string without creating a new object, you can use the system. Text. stringbuilder class. For example, when many strings are connected together in a loop, using the stringbuilder class can improve performance.

By initializing variables using an overloaded constructor method, you can create a new instance of the stringbuilder class, as described in the following example.

[C #]
Stringbuilder mystringbuilder = new stringbuilder ("Hello world! ");

Set capacity and length
Although the stringbuilder object is a dynamic object that allows you to expand the number of characters in its encapsulated string, you can specify a value for the maximum number of characters it can accommodate. This value is called the capacity of the object and should not be confused with the string length of the current stringbuilder object. For example, you can create a new instance of the stringbuilder class with the string "hello" (Length: 5) and specify the maximum capacity of this object to 25. When you modify stringbuilder, it does not re-allocate space for itself until the capacity is reached. When the capacity is reached, a new space is automatically allocated and the capacity is doubled. You can use one of the overloaded constructors to specify the capacity of the stringbuilder class. BelowCodeThe example specifies that the mystringbuilder object can be expanded to a maximum of 25 white spaces.

[C #]
Stringbuilder mystringbuilder = new stringbuilder ("Hello world! ", 25 );

In addition, you can use the read/write capacity attribute to set the maximum length of an object. The following code uses the capacity attribute to define the maximum length of an object.

[C #]
Mystringbuilder. capacity = 25;

The ensurecapacity method can be used to check the current capacity of the stringbuilder. If the capacity is greater than the passed value, no changes are made. However, if the capacity is smaller than the passed value, the current capacity is changed to match the passed value.

You can also view or set the Length attribute. If you set the Length attribute to a value greater than the capacity attribute, the capacity attribute is automatically changed to the same value as the Length attribute. If you set the Length attribute to a value smaller than the string length in the current stringbuilder object, the string is shortened.

Modify stringbuilder string
The following table lists the methods that can be used to modify the content of stringbuilder.

Use method name
Stringbuilder. append: append the information to the end of the current stringbuilder.
Stringbuilder. appendformat replaces the format specifier passed in the string with formatted text.
Stringbuilder. Insert inserts a string or object into the specified index of the current stringbuilder object.
Stringbuilder. Remove removes a specified number of characters from the current stringbuilder object.
Stringbuilder. Replace replaces the specified character at the specified index.

Append
The append method can be used to add the string representation of a text or object to the end of a string represented by the current stringbuilder object. In the following example, A stringbuilder object is initialized as "Hello World" and some text is appended to the end of the object. Space will be automatically allocated as needed.

[C #]
Stringbuilder mystringbuilder = new stringbuilder ("Hello world! ");
Mystringbuilder. append ("What a beautiful day .");
Console. writeline (mystringbuilder );

In this example, hello World! What a beautiful day. displayed on the console.

Appendformat
The appendformat method adds text to the end of stringbuilder and implements the iformattable interface. Therefore, the standard format string described in the formatting section is acceptable. You can use this method to customize the variable format and append these values to the end of stringbuilder. The following example uses the appendformat method to place an integer set to the currency value format at the end of stringbuilder.

[C #]
Int Myint = 25;
Stringbuilder mystringbuilder = new stringbuilder ("Your total is ");
Mystringbuilder. appendformat ("{0: c}", Myint );
Console. writeline (mystringbuilder );

In this example, the your total is $25.00 is displayed on the console.

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 to the sixth position of stringbuilder.

[C #]
Stringbuilder mystringbuilder = new stringbuilder ("Hello world! ");
Mystringbuilder. insert (6, "beautiful ");
Console. writeline (mystringbuilder );

In this example, hello beautiful world! Displayed on the console.

Remove
You can use the remove method to remove a specified number of characters from the current stringbuilder. The removal process starts from the specified index starting from scratch. The following example uses the Remove Method to shorten stringbuilder.

[C #]
Stringbuilder mystringbuilder = new stringbuilder ("Hello world! ");
Mystringbuilder. Remove (5, 7 );
Console. writeline (mystringbuilder );

In this example, hello is displayed on the console.

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

[C #]
Stringbuilder mystringbuilder = new stringbuilder ("Hello world! ");
Mystringbuilder. Replace ('! ','? ');
Console. writeline (mystringbuilder );

In this example, hello World? Displayed on the console.

Thank you.
The difference between stringbuffer and string in Java is the same.

========

 

Http://bbs.csdn.net/topics/60009524

 

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.