js实现C#的StringBuilder效果完整实例


Posted in Javascript onDecember 22, 2015

本文实例讲述了js实现C#的StringBuilder效果。分享给大家供大家参考,具体如下:

/*
  ##################### DO NOT MODIFY THIS HEADER #####################
  # Title: StringBuilder Class                    #
  # Description: Simulates the C# StringBuilder Class in Javascript. #
  # Author: Adam Smith                        #
  # Email: ibulwark@hotmail.com                    #
  # Date: November 12, 2006                      #
  #####################################################################
*/
// Simulates the C# StringBuilder Class in Javascript.
// Parameter["stringToAdd"] - The string to add. 
StringBuilder = function(stringToAdd)
{  
  var h = new Array();
  if(stringToAdd){h[0] = stringToAdd;} 
  this.Append = Append;
  this.AppendLine = AppendLine;
  this.ToString = ToString;
  this.Clear = Clear;
  this.Length = Length;
  this.Replace = Replace;
  this.Remove = Remove;
  this.Insert = Insert;
  this.GetType = GetType;   
  // Appends the string representation of a specified object to the end of this instance.
  // Parameter["stringToAppend"] - The string to append. 
  function Append(stringToAppend)
  {
    h[h.length] = stringToAppend;
  } 
  // Appends the string representation of a specified object to the end of this instance with a carriage return and line feed.
  // Parameter["stringToAppend"] - The string to append. 
  function AppendLine(stringToAppend)
  {
    h[h.length] = stringToAppend;
    h[h.length] = "\r\n";
  } 
  // Converts a StringBuilder to a String.
  function ToString()
  {
    if(!h){ return ""; }
    if(h.length<2){ return (h[0])?h[0]:""; }
    var a = h.join('');
    h = new Array();
    h[0] = a;
    return a;
  }
  // Clears the StringBuilder
  function Clear()
  {
    h = new Array();
  }
  // Gets the StringBuilder Length
  function Length()
  {
    if(!h){return 0;}
    if(h.length<2){ return (h[0])?h[0].length:0; }
    var a = h.join('');
    h = new Array();
    h[0] = a;
    return a.length;
  }
  // Replaces all occurrences of a specified character or string in this instance with another specified character or string.
  // Parameter["oldValue"] - The string to replace. 
  // Parameter["newValue"] - The string that replaces oldValue. 
  // Parameter["caseSensitive"] - True or false for case replace.
  // Return Value - A reference to this instance with all instances of oldValue replaced by newValue.
  function Replace(oldValue, newValue, caseSensitive)
  {
    var r = new RegExp(oldValue,(caseSensitive==true)?'g':'gi');
    var b = h.join('').replace(r, newValue);
    h = new Array();
    h[0] = b;
    return this;
  }
  // Removes the specified range of characters from this instance.
  // Parameter["startIndex"] - The position where removal begins. 
  // Parameter["length"] - The number of characters to remove.
  // Return Value - A reference to this instance after the excise operation has occurred.
  function Remove(startIndex, length)
  {    
    var s = h.join('');
    h = new Array();
    if(startIndex<1){h[0]=s.substring(length, s.length);}
    if(startIndex>s.length){h[0]=s;}
    else
    {
      h[0]=s.substring(0, startIndex); 
      h[1]=s.substring(startIndex+length, s.length);
    }
    return this;
  }
  // Inserts the string representation of a specified object into this instance at a specified character position.
  // Parameter["index"] - The position at which to insert.
  // Parameter["value"] - The string to insert. 
  // Return Value - A reference to this instance after the insert operation has occurred.
  function Insert(index, value)
  {
    var s = h.join('');
    h = new Array();
    if(index<1){h[0]=value; h[1]=s;}
    if(index>=s.length){h[0]=s; h[1]=value;}
    else
    {
      h[0]=s.substring(0, index); 
      h[1]=value; 
      h[2]=s.substring(index, s.length);
    }
    return this;
  }
  // Gets the type
  function GetType()
  {
    return "StringBuilder";
  }
};

希望本文所述对大家JavaScript程序设计有所帮助。

Javascript 相关文章推荐
javascript EXCEL 操作类代码
Jul 30 Javascript
JS模拟的QQ面板上的多级可展开的菜单
Oct 10 Javascript
jQuery 1.5 源码解读 面向中高阶JSER
Apr 05 Javascript
基于jquery的多彩百分比 动态进度条 投票效果显示效果实现代码
Aug 28 Javascript
js渐变显示渐变消失示例代码
Aug 01 Javascript
AngularJs ng-repeat 嵌套如何获取外层$index
Sep 21 Javascript
JavaScript实现自动切换图片代码
Oct 11 Javascript
javascript实现获取图片大小及图片等比缩放的方法
Nov 24 Javascript
AngularJS实现的select二级联动下拉菜单功能示例
Oct 25 Javascript
vue.js 微信支付前端代码分享
Feb 10 Javascript
Taro集成Redux快速上手的方法示例
Jun 21 Javascript
vue获取验证码倒计时组件
Aug 26 Javascript
JavaScript判断对象是否为数组
Dec 22 #Javascript
javascript中类的定义方式详解(四种方式)
Dec 22 #Javascript
jquery获取select选中值的方法分析
Dec 22 #Javascript
JS设置下拉列表框当前所选值的方法
Dec 22 #Javascript
点评js异步加载的4种方式
Dec 22 #Javascript
JS模拟按钮点击功能的方法
Dec 22 #Javascript
jquery插件jquery.confirm弹出确认消息
Dec 22 #Javascript
You might like
WindowsXP中快速配置Apache+PHP5+Mysql
2008/06/05 PHP
PHP取进制余数函数代码
2012/01/19 PHP
PHP抽奖算法程序代码分享
2015/10/08 PHP
phpstudy的php版本自由修改的方法
2017/10/18 PHP
jquery实现通用版鼠标经过淡入淡出效果
2014/06/15 Javascript
jQuery截取指定长度字符串代码
2014/08/21 Javascript
jQuery实现贪吃蛇小游戏(附源码下载)
2017/03/04 Javascript
基于javascript的异步编程实例详解
2017/04/10 Javascript
Vue 项目部署到服务器的问题解决方法
2017/12/05 Javascript
详解vue数组遍历方法forEach和map的原理解析和实际应用
2018/11/15 Javascript
构建Vue大型应用的10个最佳实践(小结)
2019/11/07 Javascript
vue子组件改变父组件传递的prop值通过sync实现数据双向绑定(DEMO)
2020/02/01 Javascript
javascript 使用sleep函数的常见方法详解
2020/04/26 Javascript
简单上手Python中装饰器的使用
2015/07/12 Python
浅谈Python中的可变对象和不可变对象
2017/07/07 Python
Python安装Numpy和matplotlib的方法(推荐)
2017/11/02 Python
PyQt5 QTable插入图片并动态更新的实例
2019/06/18 Python
python读写csv文件实例代码
2019/07/05 Python
django框架模型层功能、组成与用法分析
2019/07/30 Python
Python使用itchat模块实现简单的微信控制电脑功能示例
2019/08/26 Python
Django通用类视图实现忘记密码重置密码功能示例
2019/12/17 Python
python中类与对象之间的关系详解
2020/12/16 Python
html5中audio支持音频格式的解决方法
2018/08/24 HTML / CSS
智能旅行箱:Horizn Studios
2018/04/30 全球购物
英国和爱尔兰最大的地毯零售商:Kukoon
2018/12/17 全球购物
AVI-8手表美国官方商店:AVI-8 USA
2019/04/10 全球购物
英国门把手公司:Door Handle Company
2019/05/12 全球购物
硅酸盐工业控制专业应届生求职信
2013/11/02 职场文书
业务员岗位职责范本
2013/12/15 职场文书
老公爱的承诺书
2014/03/31 职场文书
给小学生的新年寄语
2014/04/04 职场文书
个人授权委托书
2014/09/15 职场文书
社区个人对照检查材料(群众路线)
2014/09/26 职场文书
乡镇2014法制宣传日活动总结
2014/11/01 职场文书
MySQL数据库如何给表设置约束详解
2022/03/13 MySQL
MySQL优化及索引解析
2022/03/17 MySQL