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 相关文章推荐
IE和firefox浏览器的event事件兼容性汇总
Dec 06 Javascript
通过jquery 获取URL参数并进行转码
Aug 18 Javascript
使用js画图之饼图
Jan 12 Javascript
JavaScript实现SHA-1加密算法的方法
Mar 11 Javascript
Vue.js实现一个自定义分页组件vue-paginaiton
Sep 05 Javascript
jQuery实现链接的title快速出现的方法
Feb 20 Javascript
JavaScript 数据类型详解
Mar 13 Javascript
js禁止浏览器页面后退功能的实例(推荐)
Sep 01 Javascript
JS脚本加载后执行相应回调函数的操作方法
Feb 28 Javascript
koa-router源码学习小结
Sep 07 Javascript
vue项目打包之后背景样式丢失的解决方案
Jan 17 Javascript
原生JS实现相邻月份日历
Oct 13 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 变量定义和变量替换的方法
2009/07/30 PHP
linux iconv方法的使用
2011/10/01 PHP
屏蔽机器人从你的网站搜取email地址的php代码
2012/11/14 PHP
PHP+Ajax检测用户名或邮件注册时是否已经存在实例教程
2014/08/23 PHP
PHP获取youku视频真实flv文件地址的方法
2014/12/23 PHP
php实现文件与16进制相互转换的方法示例
2017/02/16 PHP
Thinkphp开发--集成极光推送
2017/09/15 PHP
PHP中PDO事务处理操作示例
2018/05/02 PHP
PHP时间日期增减操作示例【date strtotime实现加一天、加一月等操作】
2018/12/21 PHP
yii2的restful api路由实例详解
2019/05/14 PHP
Prototype Object对象 学习
2009/07/12 Javascript
点击弹出层效果&amp;弹出窗口后网页背景变暗效果的实现代码
2014/02/10 Javascript
一个可以增加和删除行的table并可编辑表格中内容
2014/06/16 Javascript
JavaScript将一个数组插入到另一个数组的方法
2015/03/19 Javascript
JavaScript中toString()方法的使用详解
2015/06/05 Javascript
老司机带你解读jQuery插件开发流程
2016/05/16 Javascript
全面解析JavaScript中“&amp;&amp;”和“||”操作符(总结篇)
2016/07/18 Javascript
js如何找出字符串中的最长回文串
2018/06/04 Javascript
vue实现给div绑定keyup的enter事件
2020/07/31 Javascript
[01:06:12]VP vs NIP 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/17 DOTA
Python自动化测试ConfigParser模块读写配置文件
2016/08/15 Python
基于Django的ModelForm组件(详解)
2017/12/07 Python
详解用python实现简单的遗传算法
2018/01/02 Python
Python实现简易Web爬虫详解
2018/01/03 Python
python字典快速保存于读取的方法
2018/03/23 Python
浅析Python四种数据类型
2018/09/26 Python
Python 运行 shell 获取输出结果的实例
2019/01/07 Python
基于Python获取照片的GPS位置信息
2020/01/20 Python
python实现用户名密码校验
2020/03/18 Python
一款利用html5和css3实现的3D立方体旋转效果教程
2016/04/26 HTML / CSS
美国最顶级的精品店之一:Hampden Clothing
2016/12/22 全球购物
教师自我剖析材料(四风问题)
2014/09/30 职场文书
公安四风对照检查材料思想汇报
2014/10/11 职场文书
秋菊打官司观后感
2015/06/03 职场文书
教师信息技术学习心得体会
2016/01/21 职场文书
护理培训心得体会
2016/01/22 职场文书