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面向对象编程
Mar 02 Javascript
js操作ajax返回的json的注意问题!
Feb 23 Javascript
5秒后跳转效果(setInterval/SetTimeOut)
May 03 Javascript
JavaScript中的普通函数与构造函数比较
Apr 07 Javascript
JavaScript中解析JSON数据的三种方法
Jul 03 Javascript
JAVA四种基本排序方法实例总结
Jul 24 Javascript
关于事件mouseover ,mouseout ,mouseenter,mouseleave的区别
Oct 12 Javascript
jquery仿京东商品放大浏览页面
Jun 06 jQuery
jQuery实现炫丽的3d旋转星空效果
Jul 04 jQuery
angularJs复选框checkbox选中进行ng-show显示隐藏的方法
Oct 08 Javascript
Vue实战教程之仿肯德基宅急送App
Jul 19 Javascript
JavaScript代码模拟鼠标自动点击事件示例
Aug 07 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
浅析HTTP消息头网页缓存控制以及header常用指令介绍
2013/06/28 PHP
如何修改和添加Apache的默认站点目录
2013/07/05 PHP
php+ajax实现图片文件上传功能实例
2014/06/17 PHP
php简单实现发送带附件的邮件
2015/06/10 PHP
PHP随机数函数rand()与mt_rand()的讲解
2019/03/25 PHP
Javascript 遍历对象中的子对象
2009/07/03 Javascript
js计算精度问题小结
2013/04/22 Javascript
使用js 设置url参数
2013/07/08 Javascript
JavaScript中的prototype.bind()方法介绍
2014/04/04 Javascript
Lab.js初次使用笔记
2015/02/28 Javascript
详解Angular的内置过滤器和自定义过滤器【推荐】
2016/12/26 Javascript
jQuery DateTimePicker 日期和时间插件示例
2017/01/22 Javascript
vue项目国际化vue-i18n的安装使用教程
2018/03/14 Javascript
layui获取选中行数据的实例讲解
2018/08/19 Javascript
小程序中this.setData的使用和注意事项
2019/08/28 Javascript
Python通过属性手段实现只允许调用一次的示例讲解
2018/04/21 Python
python生成每日报表数据(Excel)并邮件发送的实例
2019/02/03 Python
Python函数的参数常见分类与用法实例详解
2019/03/30 Python
Python动态声明变量赋值代码实例
2019/12/30 Python
Python3 集合set入门基础
2020/02/10 Python
python开发实例之python使用Websocket库开发简单聊天工具实例详解(python+Websocket+JS)
2020/03/18 Python
Python Django form 组件动态从数据库取choices数据实例
2020/05/19 Python
django rest framework 过滤时间操作
2020/07/12 Python
详解Python流程控制语句
2020/10/28 Python
localStorage的过期时间设置的方法详解
2018/11/26 HTML / CSS
体育教育个人自荐信范文
2013/12/01 职场文书
幼儿园新学期寄语
2014/01/18 职场文书
村居抓节水倡议书
2014/05/19 职场文书
团支部推优材料
2014/05/21 职场文书
2014群众路线学习笔记
2014/11/06 职场文书
2015年教师节贺卡寄语
2015/03/24 职场文书
国庆阅兵观后感
2015/06/15 职场文书
手把手教你制定暑期学习计划,让你度过充实的暑假
2019/08/22 职场文书
几款流行的HTML5 UI框架比较(小结)
2021/04/08 HTML / CSS
win10系统xps文件怎么打开?win10打开xps文件的两种操作方法
2022/07/23 数码科技
Elasticsearch6.2服务器升配后的bug(避坑指南)
2022/09/23 Servers