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中 常用的选择器介绍
Apr 16 Javascript
jQuery javaScript捕获回车事件(示例代码)
Nov 07 Javascript
谈一谈js中的执行环境及作用域
Mar 30 Javascript
JavaScript中如何使用cookie实现记住密码功能及cookie相关函数介绍
Nov 10 Javascript
jQuery html表格排序插件tablesorter使用方法详解
Feb 10 Javascript
.net MVC+Bootstrap下使用localResizeIMG上传图片
Apr 21 Javascript
Angular2搜索和重置按钮过场动画
May 24 Javascript
使用 jQuery 实现表单验证功能
Jul 05 jQuery
微信小程序实现的自定义分享功能示例
Feb 12 Javascript
vue 实现移动端键盘搜索事件监听
Nov 06 Javascript
JavaScript实现简易聊天对话框(加滚动条)
Feb 10 Javascript
如何在selenium中使用js实现定位
Aug 18 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
自制短波长线天线频率预选器 - 成功消除B2K之流的镜像
2021/03/02 无线电
CI框架中集成CKEditor编辑器的教程
2014/06/09 PHP
PHP编写文件多服务器同步程序
2016/07/02 PHP
解决Laravel5.2 Auth认证退出失效的问题
2019/10/14 PHP
Laravel5.5 数据库迁移:创建表与修改表示例
2019/10/23 PHP
js 数据类型转换总结笔记
2011/01/17 Javascript
简单的js表单验证函数
2013/10/28 Javascript
jquery获得keycode的示例代码
2013/12/30 Javascript
捕获和分析JavaScript Error的方法
2014/03/25 Javascript
jQuery中appendTo()方法用法实例
2015/01/08 Javascript
jQuery移动web开发之页面跳转和加载外部页面的实现
2015/12/04 Javascript
JQuery ztree带筛选、异步加载实例讲解
2016/02/25 Javascript
JS匿名函数实例分析
2016/11/26 Javascript
JS数字千分位格式化实现方法总结
2016/12/16 Javascript
从零开始学习Node.js系列教程一:http get和post用法分析
2017/04/13 Javascript
js前端导出Excel的方法
2017/11/01 Javascript
微信小程序使用二次贝塞尔曲线画波浪
2018/12/25 Javascript
在React中写一个Animation组件为组件进入和离开加上动画/过度效果
2019/06/24 Javascript
如何在Angular8.0下使用ngx-translate进行国际化配置
2019/07/24 Javascript
java实现单链表增删改查的实例代码详解
2019/08/30 Javascript
JavaScript实现拖动对话框效果的实现代码
2020/10/12 Javascript
vue调用微信JSDK 扫一扫,相册等需要注意的事项
2021/01/03 Vue.js
使用python绘制常用的图表
2016/08/27 Python
python3使用pyqt5制作一个超简单浏览器的实例
2017/10/19 Python
python3+selenium自动化测试框架详解
2019/03/17 Python
Python 使用多属性来进行排序
2019/09/01 Python
Python Pickle 实现在同一个文件中序列化多个对象
2019/12/30 Python
用Python做一个久坐提醒小助手的示例代码
2020/02/10 Python
django执行原始查询sql,并返回Dict字典例子
2020/04/01 Python
Pytorch转keras的有效方法,以FlowNet为例讲解
2020/05/26 Python
写给保洁员表扬信
2014/01/08 职场文书
初一体育教学反思
2014/01/29 职场文书
幼儿园感恩节活动方案2014
2014/10/11 职场文书
2014年底工作总结
2014/12/15 职场文书
第一军规观后感
2015/06/12 职场文书
运动会班级前导词
2015/07/20 职场文书