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 相关文章推荐
jQuery JSON的解析方式分享
Apr 05 Javascript
JavaScript中instanceof运算符的用法总结
Nov 19 Javascript
深入探寻seajs的模块化与加载方式
Apr 14 Javascript
javascript实现查找数组中最大值方法汇总
Feb 13 Javascript
基于Javascript实现二级联动菜单效果
Mar 04 Javascript
最丑的时钟效果!js canvas时钟制作方法
Aug 15 Javascript
JavaScript重定向URL参数的两种方法小结
Oct 19 Javascript
JS控制TreeView的结点选择
Nov 11 Javascript
Vue2.0仿饿了么webapp单页面应用详细步骤
Jul 08 Javascript
微信小程序配置服务器提示验证token失败的解决方法
Apr 03 Javascript
微信小程序复选框实现多选一功能过程解析
Feb 14 Javascript
JavaScript实现拖动对话框效果的实现代码
Oct 12 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
DSP接收机前端设想
2021/03/02 无线电
Linux Apache PHP Oracle 安装配置(具体操作步骤)
2013/06/17 PHP
php 批量生成html,txt文件的实现代码
2013/06/26 PHP
php解析html类库simple_html_dom(详细介绍)
2013/07/05 PHP
php中rename函数用法分析
2014/11/15 PHP
PHP弱类型的安全问题详细总结
2016/09/25 PHP
PHP7原生MySQL数据库操作实现代码
2020/07/03 PHP
一个基于jquery的图片切换效果
2010/07/06 Javascript
JavaScript事件委托的技术原理探讨示例
2014/04/17 Javascript
js实现网页自动刷新可制作节日倒计时效果
2014/05/27 Javascript
js实现在同一窗口浏览图片
2014/09/17 Javascript
easyui validatebox验证
2016/04/29 Javascript
全面解析Bootstrap中form、navbar的使用方法
2016/05/30 Javascript
浅析Javascript中bind()方法的使用与实现
2016/05/30 Javascript
jquery attr()设置和获取属性值实例教程
2016/09/25 Javascript
AngularJS表单和输入验证实例
2016/11/02 Javascript
ES6中Array.find()和findIndex()函数的用法详解
2017/09/16 Javascript
AngularJs ng-change事件/指令的用法小结
2017/11/01 Javascript
详解angular应用容器化部署
2018/08/14 Javascript
CKEditor4配置与开发详细中文说明文档
2018/10/08 Javascript
JavaScript十大取整方法实例教程
2020/12/03 Javascript
[06:07]辉夜杯现场观众互动 “比谁远送显示器”
2015/12/26 DOTA
[03:21]【TI9纪实】Old Boys
2019/08/23 DOTA
python装饰器初探(推荐)
2016/07/21 Python
Django模板变量如何传递给外部js调用的方法小结
2017/07/24 Python
Python图像处理库PIL的ImageEnhance模块使用介绍
2020/02/26 Python
美国批发零售网站:GearXS
2016/07/26 全球购物
报关简历自我评价怎么写
2013/09/19 职场文书
企划经理的岗位职责
2013/11/17 职场文书
实习教师自我鉴定
2013/12/12 职场文书
生物医学工程专业学生求职信范文分享
2013/12/14 职场文书
医院总经理职责
2013/12/26 职场文书
服装店营销方案
2014/03/10 职场文书
《玩出了名堂》教学反思
2016/02/17 职场文书
员工保密协议范本,您一定得收藏!很有用!
2019/08/08 职场文书
TV动画《史上最强大魔王转生为村民A》番宣CM公布
2022/04/01 日漫