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 相关文章推荐
把html页面的部分内容保存成新的html文件的jquery代码
Nov 12 Javascript
精通Javascript系列之Javascript基础篇
Jun 07 Javascript
js获得指定控件输入光标的坐标兼容IE,Chrome,火狐等多种主流浏览器
May 21 Javascript
jQuery+JSON+jPlayer实现QQ空间音乐查询功能示例
Jun 17 Javascript
js检测浏览器版本、核心、是否移动端示例
Apr 24 Javascript
JavaScript实现三阶幻方算法谜题解答
Dec 29 Javascript
JavaScript检查弹出窗口是否被阻拦的方法技巧
Mar 13 Javascript
全面解析Bootstrap中form、navbar的使用方法
May 30 Javascript
JavaScript探测CSS动画是否已经完成的方法
Aug 30 Javascript
JS闭包可被利用的常见场景小结
Apr 09 Javascript
微信小程序实现自定义picker选择器弹窗内容
May 26 Javascript
详解微信小程序scroll-view横向滚动的实践踩坑及隐藏其滚动条的实现
Mar 14 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
DC最新动画电影:《战争之子》为何内容偏激,毁了一个不错的漫画
2020/04/09 欧美动漫
咖啡与牛奶
2021/03/03 冲泡冲煮
使用字符串函数输出整数化的PHP版本号
2006/10/09 PHP
详解YII关联查询
2016/01/10 PHP
PHP实现JS中escape与unescape的方法
2016/07/11 PHP
Yii 框架使用Forms操作详解
2020/05/18 PHP
Javascript Math对象
2009/08/13 Javascript
JavaScript 加号(+)运算符号
2009/12/06 Javascript
JavaScript包装对象使用介绍
2013/08/29 Javascript
jQuery中[attribute!=value]选择器用法实例
2014/12/31 Javascript
JavaScript静态类型检查工具FLOW简介
2015/01/06 Javascript
深入学习jQuery Validate表单验证
2016/01/18 Javascript
js拖拽的原型声明和用法总结
2016/04/04 Javascript
Knockoutjs 学习系列(二)花式捆绑
2016/06/07 Javascript
详解AngularJS如何实现跨域请求
2016/08/22 Javascript
Jquery遍历select option和添加移除option的实现方法
2016/08/26 Javascript
JS前端开发判断是否是手机端并跳转操作(小结)
2017/02/05 Javascript
Angular5给组件本身的标签添加样式class的方法
2018/04/07 Javascript
在webstorm开发微信小程序之使用阿里自定义字体图标的方法
2018/11/15 Javascript
vue.js实现会动的简历(包含底部导航功能,编辑功能)
2019/04/08 Javascript
小小聊天室Python代码实现
2016/08/17 Python
django rest framework 实现用户登录认证详解
2019/07/29 Python
使用python切片实现二维数组复制示例
2019/11/26 Python
Python函数的迭代器与生成器的示例代码
2020/06/18 Python
python实现scrapy爬虫每天定时抓取数据的示例代码
2021/01/27 Python
html5定位并在百度地图上显示的示例
2014/04/27 HTML / CSS
意大利独特而优质的家居用品:Fazzini
2018/12/05 全球购物
印尼购物网站:iLOTTE
2019/10/16 全球购物
Nike俄罗斯官方网站:Nike RU
2021/03/05 全球购物
生产车间主管岗位职责
2013/12/28 职场文书
运动会通讯稿500字
2014/02/20 职场文书
商场主管竞聘书
2014/03/31 职场文书
廉政承诺书
2015/01/19 职场文书
MySQL中存储时间的最佳实践指南
2021/07/01 MySQL
redis复制有可能碰到的问题汇总
2022/04/03 Redis
WinServer2012搭建DNS服务器的方法步骤
2022/06/10 Servers