Javascript String endsWith(search, position)

Description

Javascript String endsWith(search, position)


String.prototype.endsWith = function(search, position) {
 
 var index = (this.length -1) - (search.length - 1);

 if ( arguments.length > 1 ) {

  return this.substr( 0, position ).endsWith( search );

 }
 
 if ( this.substr(index) === search ) {
 
  return true;/*from ww  w  .ja  v  a  2s. c om*/
 
 }
  
  return false;

};

Javascript String endsWith(search, position)

String.prototype.endsWith = function(search, position) {
  var endIdx, searchIdx = search.length - 1;
  if (position){/*  ww  w  .ja  v  a2s  .  c o  m*/
    endIdx = position - 1;
  } else {
    endIdx = this.length - 1;
  }
  while(searchIdx !== 0){
    if (search[searchIdx] !== this[endIdx]){
      return false;
    }
    searchIdx -= 1;
    endIdx -= 1;
  }
  return true;
}



PreviousNext

Related