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 操作表格实现代码(多种操作打包)
Mar 20 Javascript
本地图片预览(支持IE6/IE7/IE8/Firefox3)经验总结
Mar 25 Javascript
jQuery中find()方法用法实例
Jan 07 Javascript
jQuery中DOM树操作之使用反向插入方法实例分析
Jan 23 Javascript
关于javascript中dataset的问题小结
Nov 16 Javascript
javascript创建对象、对象继承的实用方式详解
Mar 08 Javascript
你知道setTimeout是如何运行的吗?
Aug 16 Javascript
通过原生JS实现为元素添加事件的方法
Nov 23 Javascript
详解angular2封装material2对话框组件
Mar 03 Javascript
vue 集成jTopo 处理方法
Aug 07 Javascript
vuex实现数据状态持久化
Nov 11 Javascript
js编写简易的计算器
Jul 29 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
使用 php4 加速 web 传输
2006/10/09 PHP
php清除和销毁session的方法分析
2015/03/19 PHP
PHP学习笔记之session
2018/05/06 PHP
JS获取IUSR_机器名和IWAM_机器名帐号的密码
2006/12/06 Javascript
Javascript 匿名函数及其代码模式原理
2010/03/19 Javascript
js 金额文本框实现代码
2012/02/14 Javascript
仅IE支持clearAttributes/mergeAttributes方法使用介绍
2012/05/04 Javascript
iframe子父页面调用js函数示例
2013/11/07 Javascript
javascript模拟枚举的简单实例
2014/03/06 Javascript
浅析Node在构建超媒体API中的作用
2014/07/30 Javascript
JS实现让访问者自助选择网页文字颜色的方法
2015/02/24 Javascript
javascript实现图片自动和可控的轮播切换特效
2015/04/13 Javascript
深入理解JavaScript中的箭头函数
2015/07/28 Javascript
js验证身份证号有效性并提示对应信息
2015/10/19 Javascript
JS获取中文拼音首字母并通过拼音首字母快速查找页面内对应中文内容的方法【附demo源码】
2016/08/19 Javascript
详解Node.js access_token的获取、存储及更新
2017/06/20 Javascript
Three.js利用性能插件stats实现性能监听的方法
2017/09/25 Javascript
浅谈es6语法 (Proxy和Reflect的对比)
2017/10/24 Javascript
vuex实现的简单购物车功能示例
2019/02/13 Javascript
微信小程序实现搜索功能并跳转搜索结果页面
2019/05/18 Javascript
python定时器使用示例分享
2014/02/16 Python
基于Python pip用国内镜像下载的方法
2018/06/12 Python
基于python解线性矩阵方程(numpy中的matrix类)
2019/10/21 Python
python将logging模块封装成单独模块并实现动态切换Level方式
2020/05/12 Python
python drf各类组件的用法和作用
2021/01/12 Python
Kathmandu英国网站:新西兰户外运动品牌
2017/03/27 全球购物
End Clothing美国站:英国男士潮牌商城
2018/04/20 全球购物
ktv周年庆活动方案
2014/08/18 职场文书
会议接待欢迎标语
2014/10/08 职场文书
检讨书1000字
2014/10/11 职场文书
领导四风问题整改措施思想汇报
2014/10/13 职场文书
中班上学期个人总结
2015/02/12 职场文书
岗位聘任报告
2015/03/02 职场文书
院系推荐意见
2015/06/05 职场文书
我爱我班主题班会
2015/08/13 职场文书
Python语言规范之Pylint的详细用法
2021/06/24 Python