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 相关文章推荐
firefox插件Firebug的使用教程
Jan 02 Javascript
JavaScript 空位补零实现代码
Feb 26 Javascript
游览器中javascript的执行过程(图文)
May 20 Javascript
用jquery实现点击栏目背景色改变
Dec 10 Javascript
页面刷新时记住滚动条的位置jquery代码
Jun 17 Javascript
javascript+canvas制作九宫格小程序
Dec 28 Javascript
JS控制弹出新页面窗口位置和大小的方法
Mar 02 Javascript
FullCalendar日历插件应用之数据展现(一)
Dec 23 Javascript
如何通过js实现图片预览功能【附实例代码】
Mar 30 Javascript
Bootstrap Modal遮罩弹出层(完整版)
Nov 21 Javascript
jQuery实现 上升、下降、删除、添加一行代码
Mar 06 Javascript
vue2.0 + ele的循环表单及验证字段方法
Sep 18 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
php5 图片验证码实现代码
2009/12/11 PHP
thinkphp中多表查询中防止数据重复的sql语句(必看)
2016/09/22 PHP
PHP中的浅复制与深复制的实例详解
2017/10/26 PHP
PHP封装的简单连接MongoDB类示例
2019/02/13 PHP
PHP html_entity_decode()函数讲解
2019/02/25 PHP
javascript之卸载鼠标事件的代码
2007/05/14 Javascript
js中将多个语句写成一个语句的两种方法小结
2007/12/08 Javascript
js下通过getList函数实现分页效果的代码
2010/09/17 Javascript
jWiard 基于JQuery的强大的向导控件介绍
2011/10/28 Javascript
javascript中获取下个月一号,是星期几
2012/06/01 Javascript
早该知道的7个JavaScript技巧
2013/03/27 Javascript
JS实现列表页面隔行变色效果
2017/03/25 Javascript
js生成word中图片处理方法
2018/01/06 Javascript
nodejs遍历文件夹下并操作HTML/CSS/JS/PNG/JPG的方法
2018/11/01 NodeJs
Nodejs核心模块之net和http的使用详解
2019/04/02 NodeJs
微信小程序实现拖拽功能
2019/09/26 Javascript
关于ligerui子页面关闭后,父页面刷新,重新加载的方法
2019/09/27 Javascript
JavaScript enum枚举类型定义及使用方法
2020/05/15 Javascript
详解JavaScript 作用域
2020/07/14 Javascript
Openlayers学习之加载鹰眼控件
2020/09/28 Javascript
python中函数总结之装饰器闭包详解
2016/06/12 Python
python实现教务管理系统
2018/03/12 Python
一篇文章彻底搞懂Python中可迭代(Iterable)、迭代器(Iterator)与生成器(Generator)的概念
2019/05/13 Python
PyCharm搭建Spark开发环境实现第一个pyspark程序
2019/06/13 Python
win10环境下配置vscode python开发环境的教程详解
2019/10/16 Python
python tkinter之顶层菜单、弹出菜单实例
2020/03/04 Python
中国领先的专业演出票务网:永乐票务
2016/08/29 全球购物
夜大毕业生自我鉴定
2013/10/31 职场文书
区域销售经理职责
2013/12/22 职场文书
回门宴父母答谢词
2014/01/26 职场文书
爱国卫生月实施方案
2014/02/21 职场文书
毕业生写求职信的要点
2014/03/04 职场文书
中药学自荐信
2014/06/15 职场文书
公司委托书格式
2014/08/01 职场文书
2016学校先进党组织事迹材料
2016/02/29 职场文书
python超详细实现完整学生成绩管理系统
2022/03/17 Python