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和ff的在线文本编辑器类似ewebeditor
Dec 12 Javascript
js传参数受特殊字符影响错误的解决方法
Oct 21 Javascript
jquery实现弹出层完美居中效果
Mar 03 Javascript
Javascript基础知识(三)BOM,DOM总结
Sep 29 Javascript
jQuery的观察者模式详解
Dec 22 Javascript
JavaScript 表单处理实现代码
Apr 13 Javascript
AngularJS基础 ng-csp 指令详解
Aug 01 Javascript
easyUI combobox实现联动效果
Jan 17 Javascript
jQuery实现表单动态加减、ajax表单提交功能
Jun 08 jQuery
Vue + element 实现多选框组并保存已选id集合的示例代码
Jun 03 Javascript
浅谈element中InfiniteScroll按需引入的一点注意事项
Jun 05 Javascript
ECharts transform数据转换和dataZoom在项目中使用
Dec 24 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验证码函数的使用示例
2013/05/03 PHP
PHP延迟静态绑定示例分享
2014/06/22 PHP
Laravel下生成验证码的类
2017/11/15 PHP
laravel异步监控定时调度器实例详解
2019/06/21 PHP
php实现将数组或对象写入到文件的方法小结【三种方法】
2020/04/22 PHP
JS命名空间的另一种实现
2013/08/09 Javascript
textarea 控制输入字符字节数(示例代码)
2013/12/27 Javascript
jquery live()调用不存在的解决方法
2014/02/26 Javascript
Jquery对数组的操作技巧整理
2014/03/25 Javascript
artDialog+plupload实现多文件上传
2016/07/19 Javascript
js实现水平滚动菜单导航
2017/07/21 Javascript
详解 vue.js用法和特性
2017/10/15 Javascript
vue中的计算属性的使用和vue实例的方法示例
2017/12/04 Javascript
nodejs实现连接mongodb数据库的方法示例
2018/03/15 NodeJs
vue在手机中通过本机IP地址访问webApp的方法
2018/08/15 Javascript
JS面向对象编程基础篇(一) 对象和构造函数实例详解
2020/03/03 Javascript
详细分析React 表单与事件
2020/07/08 Javascript
[01:07:17]EG vs Optic Supermajor 败者组 BO3 第一场 6.6
2018/06/07 DOTA
python基础教程之面向对象的一些概念
2014/08/29 Python
Python解决鸡兔同笼问题的方法
2014/12/20 Python
Python中实现最小二乘法思路及实现代码
2018/01/04 Python
用Django实现一个可运行的区块链应用
2018/03/08 Python
使用pandas对两个dataframe进行join的实例
2018/06/08 Python
Python对切片命名的实现方法
2018/10/16 Python
python文件选择对话框的操作方法
2019/06/27 Python
pandas DataFrame创建方法的方式
2019/08/02 Python
opencv实现简单人脸识别
2021/02/19 Python
django admin管理工具自定义时间区间筛选器DateRangeFilter介绍
2020/05/19 Python
5款实用的python 工具推荐
2020/10/13 Python
如何用python实现一个HTTP连接池
2021/01/14 Python
德国婴儿推车和儿童安全座椅商店:BABYSHOP
2016/09/01 全球购物
工程专业毕业生自荐信范文
2013/12/25 职场文书
建筑经济管理专业求职信分享
2014/01/06 职场文书
Java框架入门之简单介绍SpringBoot框架
2021/06/18 Java/Android
JPA 通过Specification如何实现复杂查询
2021/11/23 Java/Android
Redis基本数据类型List常用操作命令
2022/06/01 Redis