Donnerstag, 10. Juni 2010

A StringBuilder for JavaScript

I am currently working on making some cool .net framework classes / functionality available with JavaScipt. When I'm done, there will be a nice js# framework ready for download (my goal is to keep it under 10kb compressed).

Well the first part I did is something I find myself frequently missing when coding with JavaScript: A StringBuilder. When working on a web project, there are loads of occasions which scream for such a nice class. So here we go, the source code:

// =============================================================================
// System.Text.StringBuilder
// =============================================================================
// Implements a StringBuilder.
// Differs from C#! Capacity and Length properties have been removed.
// Prepend has been added.
var StringBuilder = function(string) {
 var _str = (string == null) ? '' : string;
 // Appends a string to the stringbuilders internal string.
 this.append = function(string) {
  _str += string;
 };
 // Appends a formatted string to the stringbuilders internal string.
 this.appendFormat = function(string, array) {
  for(var i = 0; i < array.length; i++) {
   string = string.replace('{' + i + '}', array[i]);
  }
  _str += string;
 };
 // Inserts a string at a specified position inside a stringbuilders internal string.
 this.insert = function(string, position) {
  _str = _str.slice(0, position) + string + _str.slice(position, _str.length);
 };
 // Removes a substring of the stringbuilders internal string.
 this.remove = function(start, end) {
  _str = _str.slice(0, start) + _str.slice(end, _str.length);
 };
 // Replaces any substring inside the stringbuilders internal string.
 // Replaces any substring inside the stringbuilders internal string with ''.
 this.replace = function(oldValue, newValue) {
  newValue = (newValue == null) ? '' : newValue;
  while(_str.indexOf(oldValue) > -1) {
   _str = _str.replace(oldValue, newValue);
  }
 };
 // Returns the strinbuilders internal string.
 this.toString = function() {
  return _str;
 };
};

Well thats that, here's the HTML documentation. Question? Ideas? Comment!

System.Text.StringBuilder

Implements a StringBuilder.
Differs from C#! Capacity and Length properties have been removed.

Constructors

new StringBuilder();

new Stringbuilder(string);

.append(string)

Appends a string to the stringbuilders internal string.
var str = 'abcdef';

var str2 = 'ghijklmnop';

var str3 = 'qrstuvwxyz';



var sb = new StringBuilder();

sb.append(str);

sb.append(str2);

sb.append(str3);



sb.toString();

.appendFormat(string)

Appends a formatted string to the stringbuilders internal string.
var str = 'hello';

var str2 = 'world';



var sb = new StringBuilder();

sb.appendFormat('Oh, {0} there {1}!', [ str, str2 ]);

sb.toString();

.insert(string, position)

Inserts a string at a specified position inside a stringbuilders internal string.
var str = 'abcdef';

var str2 = 'ghijklmnop';

var str3 = 'qrstuvwxyz';



var sb = new StringBuilder();

sb.append(str);

sb.append(str3);

sb.insert(str2, 6);



sb.toString();

.remove(start, end)

Removes a substring of the stringbuilders internal string.
var sb = new StringBuilder('abcdefghijklmnopqrstuvwxyz');

sb.remove(6, 12);

sb.toString();

.replace(oldValue, newValue)

Replaces any substring inside the stringbuilders internal string.
var sb = new StringBuilder('abcdefghijklmnopqrstuvwxyz');

sb.replace('abcdef', '123456');

sb.toString();

.replace(oldValue)

Replaces any substring inside the stringbuilders internal string with ''.
var sb = new StringBuilder('abcdefghijklmnopqrstuvwxyzabcdef');

sb.replace('abcdef');

sb.toString();

.toString()

Returns the strinbuilders internal string.
var sb = new StringBuilder('abcdefghijklmnopqrstuvwxyzabcdef');

sb.toString();

3 Kommentare:

  1. Would it not be more efficient to keep an internal array of added strings instead of concatenating (as per line 11,18).

    AntwortenLöschen
  2. Why? I thought of this as well, but then I couldn't imagine any scenario in which this could be of use. Else than eating up more memory ;-)

    AntwortenLöschen

Socialize!