JavaScript - String Builder Plugin

In continuing with my theme of porting useful C# functionality to JavaScript I have desided to add C#’s StringBuilder class to JavaScript in the form of String.builder.

When simply concatenating strings the string builder class may not be of much use since a simple += will suffice but when you need to go deeper and manipulate that concatenated string then the string builder really shines.

The string builder class has 6 methods to work with.

Method Name Method Use
append(string) adds string to the end of the string
clear() clears the internal buffer
insert(index, string) inserts string into the given buffer index overriding the existing value
remove(string)
remove(index, length)
this method is overloaded
1 of 2: removes all occurances of string
2 of 2: removes stating at index and removes length number of indexes
replace(findThis, replaceWith) replaces all occurances of findThis with replaceWith
toString() returns the concatentated string

Below is the source code for this class and a simple example of it’s use.

JavaScript

if(!String.builder)
{
	String.builder = function()
	{
		var buffer = [];

		this.append = function(str)
		{
			buffer.push(str);
		}
		this.clear = function()
		{
			buffer = null;
			buffer = [];
		}
		this.insert = function(index, str)
		{
			if(buffer[index]){buffer[index] = str;}
			else{return false;}
		}
		this.replace = function(find, replace)
		{
			for(var i=0;i<buffer.length;i++)
			{
				var exp = new RegExp(find,'gm');
				buffer[i] = buffer[i].replace(exp,replace);
			}
		}
		this.remove = function()
		{
			if(typeof arguments[0] == "string")
			{
				for(var i=0;i<buffer.length;i++)
				{
					if(arguments[0] == buffer[i]){buffer.splice(i, 1);}
				}
			}
			else
			{
				var index = arguments[0];
				var length = (arguments.length > 1) ? arguments[1] : 1;
				buffer.splice(index,length);
			}
		}
		this.toString = function()
		{
			var str = "";
			for(var i=0;i<buffer.length;i++)
			{
				str += buffer[i];
			}
			return str;
		}
	}
}

HTML

<html>
<head>
	<title></title>
	<script src="StringBuilder.js"></script>
	<script>

		var sb = new String.builder();
		sb.append("hi");
		sb.append("there");
		sb.append(",");
		sb.append(" ");
		sb.append("how");
		sb.append("are");
		sb.append("you");
		sb.append("?");
		//sb.clear();
		//sb.insert(1," there");
		//sb.replace("how","how ");
		//sb.remove("how");
		//sb.remove(4,3);

		alert(sb.toString());

	</script>
</head>
<body>

</body>
</html>

3 comments on this post

Tombo says:
Jul 3, 2007 - 10:07:48

in the .toString method why not just:

return buffer.join(”");

executes much quicker

Jon says:
Jul 3, 2007 - 12:07:40

Are you aware that stringbuilder already exists in the ASP.NET AJAX client side libraries?

http://ajax.asp.net/docs/ClientReference/Sys/StringBuilderClass/default.aspx

Justin says:
Jul 3, 2007 - 07:07:36

As surprised as you might be, as a .Net developer I have just about zero experience with the ASP.Net AJAX framework. I generally use Mootools which is missing the string builder class as well as String.Format which I posted earlier. I also use the AjaxManager from Telerik.