First additions to the String object that allow you to use left(x) and right(x) just like in LS
// ****************************************
String.prototype.left = function(n){
if (n <= 0)
return "";
else if (n > this.length)
return this;
else
return this.substring(0,n);
}
// ****************************************
String.prototype.right= function(n){
if (n <= 0)
return "";
else if (n > this.length)
return this;
else {
var iLen = this.length;
return this.substring(iLen, iLen - n);
}
}
// *****************************************
and another String function setCharAt(x,y) which will replace the character at postion x with character y. BTW x starts at 0 as the first character!
for example
myString = "My Dog has no nose"
myString.setChar(3,"G")
would result in myString being equal to "My God has no nose"
String.prototype.setCharAt = function(index,chr) {
if(index > this.length-1) return str;
return this.substr(0,index) + chr + this.substr(index+1);
}
Finally for today and extension to the number object Number.padZero(x) that will return any number as string x long with left padded zeros.
For example
var myNumber = 12
var myPad = myNumber.padZero(4)
would return myNumber = "0012"
This function is useful if you are populating the DOM with objects based on a FOR loop and you want the object.id's to be a set number of characters long.
Number.prototype.padZero = function(cnt)
{
var retvar = this.toString();
cnt=cnt-retvar.length
if(cnt>0)
{
for(var x=1;x<=cnt;x++)
{
retvar = "0"+retvar;
}
}
return retvar;
}