|
Here are more functions added to string prototype
Check if string contains a substring
String.prototype.contains = function (it) { return this.indexOf(it) != -1; };
alert("Hello World".toString().contains("World"));
Check for substring or string betwen two sub stubstring
String.prototype.substringBetween = function (string1, string2) {
if ((this.indexOf(string1, 0) == -1) || (this.indexOf(string2, this.indexOf(string1, 0)) == -1)) {
return (-1);
} else {
return this.substring((this.indexOf(string1, 0) + string1.length), (this.indexOf(string2, this.indexOf(string1, 0))));
}
};
alert("Hello World This is a test".toString().substringBetween ("World","is"));
|