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来定义类的规范小结
Nov 19 Javascript
游览器中javascript的执行过程(图文)
May 20 Javascript
判断多个input type=file是否有已经选择好文件的代码
May 23 Javascript
jquery 事件冒泡的介绍以及如何阻止事件冒泡
Dec 25 Javascript
jquery中get,post和ajax方法的使用小结
Feb 04 Javascript
JavaScript实现简单Tip提示框效果
Apr 20 Javascript
js中获取 table节点各tr及td的内容简单实例
Oct 14 Javascript
ES6中Proxy代理用法实例浅析
Apr 06 Javascript
微信小程序使用form表单获取输入框数据的实例代码
May 17 Javascript
小程序tab页无法传递参数的方法
Aug 03 Javascript
详解离线安装npm包的几种方法
Nov 25 Javascript
Js视频播放器插件Video.js使用方法详解
Feb 04 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
php运行出现Call to undefined function curl_init()的解决方法
2010/11/02 PHP
基于Snoopy的PHP近似完美获取网站编码的代码
2011/10/23 PHP
PHP读取数据库并按照中文名称进行排序实现代码
2013/01/29 PHP
ThinkPHP表单自动验证实例
2014/10/13 PHP
PHP+Jquery与ajax相结合实现下拉淡出瀑布流效果【无需插件】
2016/05/06 PHP
PHP获取客户端及服务器端IP的封装类
2016/07/21 PHP
php检查函数必传参数是否存在的实例详解
2017/08/28 PHP
php读取本地json文件的实例
2018/03/07 PHP
js判断生效时间不得大于失效时间的思路及代码
2013/04/23 Javascript
extjs4 treepanel动态改变行高度示例
2013/12/17 Javascript
JavaScript中的跨浏览器事件操作的基本方法整理
2016/05/20 Javascript
Javascript基础_简单比较undefined和null 值
2016/06/14 Javascript
Vue.js 表单校验插件
2016/08/14 Javascript
Javascript Function.prototype.bind详细分析
2016/12/29 Javascript
通过示例彻底搞懂js闭包
2017/08/10 Javascript
js canvas实现橡皮擦效果
2018/12/20 Javascript
微信小程序实现多选删除列表数据功能示例
2019/01/15 Javascript
浅谈 Webpack 如何处理图片(开发、打包、优化)
2019/05/15 Javascript
关于NodeJS中的循环引用详解
2019/07/23 NodeJs
微信小程序实现横向滚动导航栏效果
2019/12/12 Javascript
在vue中使用Echarts画曲线图的示例
2020/10/03 Javascript
[48:32]2018DOTA2亚洲邀请赛 3.31 小组赛 A组 LGD vs VG
2018/04/01 DOTA
python连接sql server乱码的解决方法
2013/01/28 Python
go和python调用其它程序并得到程序输出
2014/02/10 Python
python中stdout输出不缓存的设置方法
2014/05/29 Python
Python中with及contextlib的用法详解
2017/06/08 Python
pycharm执行python时,填写参数的方法
2018/10/29 Python
使用python获取电脑的磁盘信息方法
2018/11/01 Python
在Django的View中使用asyncio的方法
2019/07/12 Python
Python实现TCP探测目标服务路由轨迹的原理与方法详解
2019/09/04 Python
python获取本周、上周、本月、上月及本季的时间代码实例
2020/09/08 Python
html特殊符号示例 html特殊字符编码对照表
2014/01/14 HTML / CSS
生物制药毕业生自荐信
2013/10/16 职场文书
管理失职检讨书
2014/02/12 职场文书
教师求职自荐书
2014/06/14 职场文书
详解ZABBIX监控ESXI主机的问题
2022/06/21 Servers