Implementation of stringbuilder class in Javascript

Source: Internet
Author: User
A simple stringbuilder class implementation
  1. // Initializes a new instance of the stringbuilder class
  2. // And appends the given value if supplied
  3. Function stringbuilder (value)
  4. {
  5. This. Strings = new array ("");
  6. This. append (value );
  7. }

  8. // Appends the given value to the end of this instance.
  9. Stringbuilder. Prototype. append = function (value)
  10. {
  11. If (value)
  12. {
  13. This. Strings. Push (value );
  14. }
  15. }

  16. // Clears the string buffer
  17. Stringbuilder. Prototype. Clear = function ()
  18. {
  19. This. Strings. Length = 1;
  20. }

  21. // Converts this instance to a string.
  22. Stringbuilder. Prototype. tostring = function ()
  23. {
  24. Return this. Strings. Join ("");
  25. }

The code looks simple and straightforward. It is actually implemented using array, push, join, etc. The following describes how to use this class

  1. // Create a stringbuilder
  2. VaR sb = new stringbuilder ();

  3. // Append some text
  4. SB. append ("some of those preparing for international ");
  5. SB. append ("exams such as the TOEFL ");
  6. SB. append ("need extra practice for the listening section ");

  7. // Get the full string value
  8. VaR S = sb. tostring ();
  9. Alert (s );

It is very simple and does not require much explanation. If you use stringbuilder in. net, you will also know how to use it.

(See more: http://www.codeproject.com/KB/scripting/stringbuilder.aspx)

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.