javascript中StringBuilder类实现

一个简单的StringBuilder类实现
<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

  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. }


代码看上去很简单直接。实际上就是用array,push,join等来实现,以下是如何使用该类

  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);

非常简单,不需要太多的说明。如果你在.NET中用了StringBuilder,你也会知道这里如何用。
<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>
(see more: http://www.codeproject.com/KB/scripting/stringbuilder.aspx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值