Skip to content

Instantly share code, notes, and snippets.

@06b
Created February 7, 2014 21:03
Show Gist options
  • Save 06b/8871805 to your computer and use it in GitHub Desktop.
Save 06b/8871805 to your computer and use it in GitHub Desktop.
StartsWith(string suffix,bool ignoreCase), EndsWith(string suffix,bool ignoreCase) and Trim() :
// string.StartWith implementation
String.prototype.StartsWith = function(prefix,ignoreCase) {
if( !prefix ) return false;
if( prefix.length > this.length ) return false;
if( ignoreCase ) {
if( ignoreCase == true ) {
return (this.substr(0, prefix.length).toUpperCase() == prefix.toUpperCase());
}
}
return (this.substr(0, prefix.length) === prefix);
}
// string.EndsWith implementation
String.prototype.EndsWith = function(suffix,ignoreCase) {
if( !suffix ) return false;
if( suffix.length > this.length ) return false;
if( ignoreCase ) {
if( ignoreCase == true ) {
return (this.substr(this.length - suffix.length).toUpperCase() == suffix.toUpperCase());
}
}
return (this.substr(this.length - suffix.length) === suffix);
}
// string.Trim implementation
String.prototype.Trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment