Get substring from String - Node.js String

Node.js examples for String:sub string

Description

Get substring from String

Demo Code


String.prototype.sub = function(n){       
    var r = /[^\x00-\xff]/g;
  if (this.length <= n){
    return this.toString();
  }//w  ww .j av  a 2 s .  c  o  m
  
    if(this.replace(r, "mm").length <= n)    
        return this.toString();      
  
    var m = n;//Math.floor(n/2);       
    for(var i=m; i<this.length; i++) {       
        if(this.substr(0, i).replace(r, "mm").length>=n) {    
            return this.substr(0, i) + "...";    
        }   
    }    
    return this.toString();      
};

Related Tutorials