Skip to content

Instantly share code, notes, and snippets.

@jeffreyiacono
Created December 10, 2012 02:39
Show Gist options
  • Select an option

  • Save jeffreyiacono/4248059 to your computer and use it in GitHub Desktop.

Select an option

Save jeffreyiacono/4248059 to your computer and use it in GitHub Desktop.
extend js String to implement basic trim methods
function strltrim() {
return this.replace(/^\s+/,'');
}
function strrtrim() {
return this.replace(/\s+$/,'');
}
function strtrim() {
return this.replace(/^\s+/,'').replace(/\s+$/,'');
}
String.prototype.ltrim = strltrim;
String.prototype.rtrim = strrtrim;
String.prototype.trim = strtrim;
var name = " middle ";
console.log("a" + name.trim() + "z"); // amiddlez
console.log("a" + name.rtrim() + "z"); // a middlez
console.log("a" + name.ltrim() + "z"); // amiddle z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment