Nodejs String Insert insert(value, position)

Here you can find the source of insert(value, position)

Method Source Code

/**/*from   w  w w.  j a  va 2s  . c  o  m*/
 * Insert a string into another one at a given numerical index or a keyword.
 *
 * @example  'I am here.'.insert('in between ', 5)
 *           'I am in between here.'
 *
 * @param    value    {String}         The string to be inserted into the other
 *                                     one.
 * @param    position {String, Number} Can either be a numerical keyword or
 *                                     'start' / 'end'.
 *
 * @return   {String} Returns the new string.
 */

String.prototype.insert = String.prototype.insert || function (value, position) {
  if (!value) return this;
  position = position === 'end' ? this.length : position === 'start' ? 0 : position || 0
  return this.slice(0, position) + value + this.slice(position)
}

Related

  1. insert(index, string)
    String.prototype.insert = function (index, string) {
      if (index > 0) {
        return this.substring(0, index) + string + this.substring(index, this.length);
      else {
        return string + this;
    };
    
  2. insert(start, new_string)
    String.prototype.insert = function(start, new_string)
      return this.slice(0, start) + new_string + this.slice(start);
    };
    String.prototype.splice = function(start, length, insert)
      return this.substring(0, start) + insert + this.substring(start + length);
    };
    Object.prototype.extend = function(other)
    ...
    
  3. insert(start, new_string)
    String.prototype.insert = function(start, new_string)
      return this.slice(0, start) + new_string + this.slice(start);
    };
    String.prototype.splice = function(start, length, insert)
      return this.substring(0, start) + insert + this.substring(start + length);
    };
    Object.prototype.extend = function(other)
    ...
    
  4. insert(start, new_string)
    String.prototype.insert = function(start, new_string)
      return this.slice(0, start) + new_string + this.slice(start);
    };
    String.prototype.splice = function(start, length, insert)
      return this.substring(0, start) + insert + this.substring(start + length);
    };
    
  5. insert(text,at)
    String.prototype.insert = function(text,at) {
        if(at == null || at > this.length)
            at = this.length;
        else if(at < 0)
            at = 0;
        return this.substring(0,at)+text+this.substring(at);
    
  6. insertAt( loc, strChunk )
    String.prototype.insertAt = function ( loc, strChunk ) {
        return (this.valueOf().substr( 0, loc )) + strChunk + (this.valueOf().substr( loc ))
    };
    
  7. insertAt(b,a)
    String.prototype.insertAt=function(b,a){
      return(this.valueOf().substr(0,b))+a+(this.valueOf().substr(b))
    };
    
  8. insertAt(idx, rem, text)
    String.prototype.insertAt = function(idx, rem, text){
      return (this.slice(0,idx) + text + this.slice(idx + Math.abs(rem)));
    };
    
  9. insertAt(index, string)
    String.prototype.insertAt=function(index, string) {
        return this.substr(0, index) + string + this.substr(index);
    };