Last active
August 29, 2015 14:10
-
-
Save gko/da4337478ea785105872 to your computer and use it in GitHub Desktop.
string functions for js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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