Let say you want to replace all spaces in string by dashes,
of course you can implement distinct function that does it:
function ReplaceByDashes(string){
return string.replace(/\s/g,'-');
}
// run it
var string = "Hello my beautiful world!";
string=ReplaceByDashes(string);
alert(string);
However, we can add our custom method to string object that already built-in in JavaScript:
if(typeof String.prototype.rbd==='undefined'){ // we have to check if someone hasn't added it already
String.prototype.rbd = function(){
return this.replace(/\s/g,'-');
}
}
// run it
var string = "Hello my beautiful world!";
string=string.rbd();
alert(string);
Some useful example, capitalize first letter of string
String.prototype.ucFirst = function () {
return (this.substr(0,1).toUpperCase() + this.substr(1,this.length));
}
Comments (3)
RSS Collapse / Expandtapin13
Waiting for more complicated stuff.
What about Ruby?
paker
t0lkman
Only registered and authorized users can leave comments. Login or Register