Skip to content

Instantly share code, notes, and snippets.

@gko
Last active August 29, 2015 14:10
Show Gist options
  • Save gko/da4337478ea785105872 to your computer and use it in GitHub Desktop.
Save gko/da4337478ea785105872 to your computer and use it in GitHub Desktop.
string functions for js
/**
* number of occurences of substring
*/
String.prototype.occurences = function(str, from, to) {
return this.substr(from||0, (to||this.length)-(from||0)).split(str).length-1;
};
/**
* whether string starts with another string
*/
String.prototype.startsWith = function(str) {
if(typeof str !== "string")
return false;
for(var i in str)
if(!this[i] || this[i] !== str[i])
return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment