Javascript String endsWith(substring)

Description

Javascript String endsWith(substring)



String.prototype.endsWith = function(substring) {
    var stringLength = this.length;
    var substringLength = substring.length;

    if (substringLength > stringLength) {
        return false;
    }//  w  ww .j  a  v  a  2  s .  co  m

    var i;
    for (i = 0; i < substringLength; i += 1) {
        if (substring[substringLength - i] !== this[stringLength - i]) {
            return false;
        }
    }

    return true;
};

Javascript String endsWith(substring)

String.prototype.endsWith = function(substring){
    return this.lastIndexOf(substring) == this.length - substring.length;
};



PreviousNext

Related