js实现C#的StringBuilder效果完整实例_javascript技巧
程序员文章站
2022-03-16 16:55:28
...
本文实例讲述了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.lengths.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=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程序设计有所帮助。
上一篇: php网站是什么?
推荐阅读
-
js实现同一页面多个运动效果的方法_javascript技巧
-
js实现点小图看大图效果的思路及示例代码_javascript技巧
-
js实现的四级左侧网站分类菜单实例_javascript技巧
-
基于JS实现PHP的sprintf函数实例_javascript技巧
-
js实现会跳动的日历效果(完整实例)
-
JS实现简单的顶部定时关闭层效果_javascript技巧
-
js 文本滚动效果的实例代码_javascript技巧
-
原生Js实现按的数据源均分时间点幻灯片效果(已封装)_javascript技巧
-
用CSS+JS实现的进度条效果效果_javascript技巧
-
js/ajax跨越访问-jsonp的原理和实例(javascript和jquery实现代码)_javascript技巧