Nodejs String Ends With endsWith(substring)

Here you can find the source of endsWith(substring)

Method Source Code

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

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

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

    return true;
};

Related

  1. endsWith(string)
    String.prototype.endsWith = function (string) {
      var index = arguments.length < 2 ? this.length : arguments[1];
      return this.indexOf(string) === index - string.length;
    };
    
  2. endsWith(string)
    String.prototype.endsWith = function (string) {
      var index = arguments.length < 2 ? this.length : arguments[1];
      var foundIndex = this.indexOf(string);
      return foundIndex !== -1 && foundIndex === index - string.length;
    };
    
  3. endsWith(string)
    String.prototype.endsWith = function (string) {
      var index = arguments.length < 2 ? this.length : arguments[1];
      var foundIndex = this.lastIndexOf(string);
      return foundIndex !== -1 && foundIndex === index - string.length;
    };
    
  4. endsWith(substr)
    String.prototype.endsWith = function(substr) {
      return (this.match(substr + "$") == substr);
    };
    
  5. endsWith(substring)
    String.prototype.endsWith = function (substring) {
        var stringCut = this.substring(this.length - substring.length);
        return stringCut === substring;
    
  6. endsWith(substring)
    String.prototype.endsWith = function(substring){
        return this.lastIndexOf(substring) == this.length - substring.length;
    };
    
  7. endsWith(substring)
    String.prototype.endsWith = function(substring) {
        var compareString = this.substring(this.length - substring.length, this.length);
        return compareString === substring;
    };
    
  8. endsWith(suffix)
    String.prototype.endsWith = function(suffix) {
        return this.indexOf(suffix, this.length - suffix.length) !== -1;
    };
    
  9. endsWith(suffix)
    'use strict';
    String.prototype.endsWith = function(suffix) {
        return this.indexOf(suffix, this.length - suffix.length) !== -1;
    };