Tags

In my previous post, I wrote an example how to check if a string is starting with a prefix. Now it is time to figure out how to check if a string ends with a suffix.

String.prototype.endWith = function string$endWith(suffix, caseSensitive) {
    var _caseSensitive = (typeof(caseSensitive) !== 'boolean') ? false : caseSensitive;
	if(_caseSensitive === true) {
		 return (this.substring(this.length - suffix.length) === suffix);
	}
	else {
		 return (this.substring(this.length - suffix.length).toLowerCase() === suffix.toLowerCase());
	}
}

Case sensitivity is taken into consideration if second parameter is true. If not, case sensitivity is ignored.

Advertisements