Difference between string and stringbuilder in C #

Source: Internet
Author: User
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. text;
  4. Namespace string
  5. {
  6. Class Program
  7. {
  8. Static void main (string [] ARGs)
  9. {
  10. // Demo of frequent memory allocation of strings (not recommended)
  11. // The following Code removes a value from the string, and the original string is replaced with the new encrypted one.
  12. // But the efficiency is low. greetingtxt is a string-type object. Each time + = is used, the original object is deleted and referenced to point to the new referenced object.
  13. // In this way, the memory is re-allocated to the string multiple times in a loop, and a large amount of discarded data will be generated in the heap for recovery ..
  14. String greetingtxt = "hello from all the guys atwrox press. zzzaaa ";
  15. For (INT I = 'Z'; I> = 'a'; I --)
  16. {
  17. Char old = (char) I;
  18. Char new1 = (char) (I + 1 );
  19. Greetingtxt = greetingtxt. Replace (old, new1 );
  20. }
  21. For (INT I = 'Z'; I> = 'a'; I --)
  22. {
  23. Char old = (char) I;
  24. Char new1 = (char) (I + 1 );
  25. Greetingtxt = greetingtxt. Replace (old, new1 );
  26. }
  27. Console. writeline (greetingtxt );
  28. Console. writeline ("stringbuilder ----------------------------------");
  29. // Stringbuilder demo
  30. // The stringbuilder class only supports the addition of General string deletion functions. It is not as efficient as string operations.
  31. // String is the size of the heap space normally allocated, while stringbuilder is usually more than the memory allocated.
  32. // Developers can also specify the amount of memory allocated by stringbuilder.
  33. // The actual length of the two important attributes. the maximum length of the capacity string storage unit (that is, the maximum memory size)
  34. // This improves the efficiency of string replacement because it can be operated in the free space allocated by stringbuilder.
  35. // However, deletion of the added string will still affect the storage size.
  36. // If you add a string that exceeds the size of the object, the object memory will be automatically revised to ensure the required
  37. // The second parameter indicates the maximum number of character objects. Generally, a maximum possible number should be set to ensure that the memory will not be expanded.
  38. Stringbuilder sbstr = new stringbuilder ("hello from all the guys atwrox press.", 150 );
  39. Console. writeline ("hello from all the guys atwrox press". Length + "string size ");
  40. // Obviously, the string size is 36, And we allocate a space of 150 characters to the SB object. In this case, we assume that the new append characters will not exceed 150-36.
  41. // The Sb object will not be re-allocated, and our efficiency will be improved.
  42. Sbstr. append ("Hello Aladdin! ");
  43. Console. writeline (sbstr );
  44. // The efficiency of Sb can be used only when files are frequently replaced. It is also an example of the Encryption above. We use Sb object to execute
  45. For (INT I = 'Z'; I> = 'a'; I --)
  46. {
  47. Char old = (char) I;
  48. Char new1 = (char) (I + 1 );
  49. Sbstr = sbstr. Replace (old, new1 );
  50. }
  51. For (INT I = 'Z'; I> = 'a'; I --)
  52. {
  53. Char old = (char) I;
  54. Char new1 = (char) (I + 1 );
  55. Sbstr = sbstr. Replace (old, new1 );
  56. }
  57. Console. writeline (sbstr + "encrypted ");
  58. // The above Code uses the replace method of the stringbuilder class. It does not need to copy strings during the process, which is highly efficient.
  59. // In addition to the two important attributes of length and capacity, the stringbuilder class also has a maxcapacity
  60. // Maxcapacity refers to the maximum number of characters. The default value is the same as that of the int type. Int. maxvalue!
  61. // However, the given value can be displayed, but an exception occurs if the given maximum value is smaller than the specified string space.
  62. // For example, if we set maxcapacity to 100 and capacity to 150, an exception occurs.
  63. Stringbuilder sb = new stringbuilder (100, 50 );
  64. // Conclusion: Generally, stringbuilder is used for string operations, while string is used for string display!
  65. Console. Readline ();
  66. }
  67. }
  68. }

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.