String and Array extensions in Atlas Runtime - a StringBuilder too!

Atlas runtime extends the String class of Javascript with the following handy extensions:

String.prototype.endsWith=function(suffix){
String.prototype.startsWith=function(prefix){
String.prototype.lTrim=String.prototype.trimLeft=function(){
String.prototype.rTrim=String.prototype.trimRight=function(){
String.prototype.trim=function(){
String.format=function(format){

Format one is pretty powerful. You can build complex strings very efficiently:

var result = String.format("Hello {0} My name {1} {2}", param1, param2, param3);

It also has a StringBuilder just like .NET. If you are building a string of HTML anywhere which has more than 10 concatenation, use StringBuilder. Concatenations are like:

Html = "...." + "...." + "...." + "....";

Or

Html += ".....";

If you are doing things like this in loops, you must use StringBuilder. Javascript string concatenation is very very slow.

StringBuilder is defined as:

Sys.StringBuilder=function(initialText){
 this.append=function(text){
 this.appendLine=function(text){
 this.clear=function(){
 this.toString=function(delimiter){

Here's how you use it:

var builder = new Sys.StringBuilder("Initial text, or leave blank");
builder.append("more text");
var result = builder.toString();

The Array class is pretty rich. It has the following public methods:

Array.prototype.add=function(item)
Array.prototype.addRange=function(items)
Array.prototype.clear=function()
Array.prototype.clone=function()
Array.prototype.contains=Array.prototype.exists=function(item)
Array.prototype.dequeue=function()
Array.prototype.indexOf=function(item,startIndex)
Array.prototype.insert=function(index,item)
Array.prototype.remove=function(item)
Array.prototype.removeAt=function(index)
Array.parse=function(value)

All these functions are equivalent to .NET Array and String class. So, I am not explaining in details what they do.

Published Wed, Aug 2 2006 13:40 by omar
Filed under: