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中删除指定数组中指定的元素的代码
Feb 12 Javascript
jQuery事件绑定.on()简要概述及应用
Feb 07 Javascript
JS读取XML文件示例代码
Nov 15 Javascript
HTML5实现留言和回复页面样式
Jul 22 Javascript
js+canvas绘制五角星的方法
Jan 28 Javascript
Bootstrap基本插件学习笔记之Alert警告框(20)
Dec 08 Javascript
ionic 3.0+ 项目搭建运行环境的教程
Aug 09 Javascript
使用nvm管理不同版本的node与npm的方法
Oct 31 Javascript
详解如何用模块化的方式写vuejs
Dec 16 Javascript
js纯前端实现腾讯cos文件上传功能的示例代码
May 14 Javascript
thinkjs微信中控之微信鉴权登陆的实现代码
Aug 08 Javascript
vue中渲染对象中属性时显示未定义的解决
Jul 31 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中常用的转义函数
2014/02/28 PHP
PHP两种去掉数组重复值的方法比较
2014/06/19 PHP
php生成4位数字验证码的实现代码
2015/11/23 PHP
thinkPHP删除前弹出确认框的简单实现方法
2016/05/16 PHP
Zend Framework常用校验器详解
2016/12/09 PHP
PHP开发实现微信退款功能示例
2017/11/25 PHP
jQuery 1.5.1 发布,全面支持IE9 修复大量bug
2011/02/26 Javascript
让低版本浏览器支持input的placeholder属性(js方法)
2013/04/03 Javascript
JavaScript实现的SHA-1加密算法完整实例
2016/02/02 Javascript
Bootstrap缩略图与警告框学习使用
2017/02/08 Javascript
Easyui Tree获取当前选择节点的所有顶级父节点
2017/02/14 Javascript
AngularJS之页面跳转Route实例代码
2017/03/10 Javascript
Vue2.0 事件的广播与接收(观察者模式)
2018/03/14 Javascript
详解ES6 Promise对象then方法链式调用
2018/10/20 Javascript
jQuery实现根据身份证号获取生日、年龄、性别等信息的方法
2019/01/09 jQuery
解决layui弹出层layer的area过大被遮挡的问题
2019/09/21 Javascript
[01:14:05]《加油DOTA》第四期
2014/08/25 DOTA
python list删除元素时要注意的坑点分享
2018/04/18 Python
Python动态赋值的陷阱知识点总结
2019/03/17 Python
Python基础教程之if判断,while循环,循环嵌套
2019/04/25 Python
python DataFrame转dict字典过程详解
2019/12/26 Python
使用pygame编写Flappy bird小游戏
2020/03/14 Python
详解python对象之间的交互
2020/09/29 Python
html5 touch事件实现触屏页面上下滑动(二)
2016/03/10 HTML / CSS
String这个类型的class为何定义成final?
2012/11/13 面试题
2014政府领导班子对照检查材料思想汇报(3篇)
2014/09/26 职场文书
2014年工会工作总结
2014/11/12 职场文书
2015年仓库工作总结
2015/04/09 职场文书
初中体育教学随笔
2015/08/15 职场文书
会议承办单位欢迎词
2015/09/30 职场文书
中学语文教学反思
2016/02/16 职场文书
2019年二手房买卖合同范本
2019/10/14 职场文书
22句经典语录:送给优柔寡断和胡思乱想的朋友们
2019/12/13 职场文书
《悲惨世界》:比天空更广阔的是人的心灵
2020/01/16 职场文书
教你怎么用Python实现多路径迷宫
2021/04/29 Python
详解解Django 多对多表关系的三种创建方式
2021/08/23 Python