js字符串处理
1 //获取字符串字节长度 2 String.prototype.charLen = function() { 3 var self = this; 4 if(""==self) return 0; 5 var l=0; 6 for(var i=0;i<self.length;i++) 7 { 8 if(self.charCodeAt(i)>255) l+=2; 9 else l++; 10 } 11 return l; 12 }; 13 14 //删除字符串左边空格 15 String.prototype.ltrim = function() { 16 var self = this; 17 if("" == self) return ''; 18 return self.replace(/(^\s*)/g,""); 19 } 20 21 //删除字符串两端空格 22 String.prototype.trim = function() { 23 var self = this; 24 if("" == self) return ''; 25 return self.replace(/(^\s*)|(\s*$)/g, ""); 26 }; 27 28 //删除字符串右边空格 29 String.prototype.ltrim = function() { 30 var self = this; 31 if("" == self) return ''; 32 return self.replace(/(\s*$)/g,""); 33 }