String 的擴充方法–轉自無憂指令碼
來源:互聯網
上載者:User
//擷取字元數組String.prototype.ToCharArray=function(){ return this.split("");}//擷取N個相同的字串String.prototype.Repeat=function(num){ var tmpArr=[]; for(var i=0;i<num;i++) tmpArr.push(this); return tmpArr.join("");}//逆序String.prototype.Reverse=function(){ return this.split("").reverse().join("");}//測試是否是數字String.prototype.IsNumeric=function(){ var tmpFloat=parseFloat(this); if(isNaN(tmpFloat)) return false; var tmpLen=this.length-tmpFloat.toString().length; return tmpFloat+"0".Repeat(tmpLen)==this;}//測試是否是整數String.prototype.IsInt=function(){ if(this=="NaN") return false; return this==parseInt(this).toString();}// 合并多個空白為一個空白String.prototype.resetBlank = function(){ return this.replace(/s+/g," ");}// 除去左邊空白String.prototype.LTrim = function(){ return this.replace(/^s+/g,""); } // 除去右邊空白String.prototype.RTrim = function(){ return this.replace(/s+$/g,""); }// 除去兩邊空白String.prototype.trim = function(){ return this.replace(/(^s+)|(s+$)/g,""); }// 保留數字String.prototype.getNum = function(){ return this.replace(/[^d]/g,"");}// 保留字母String.prototype.getEn = function(){ return this.replace(/[^A-Za-z]/g,""); }// 保留中文String.prototype.getCn = function(){ return this.replace(/[^u4e00-u9fa5uf900-ufa2d]/g,"");}// 得到位元組長度String.prototype.getRealLength = function(){ return this.replace(/[^x00-xff]/g,"--").length;}// 從左截取指定長度的字串String.prototype.left = function(n){ return this.slice(0,n);}// 從右截取指定長度的字串String.prototype.right = function(n){ return this.slice(this.length-n);}// HTML編碼String.prototype.HTMLEncode = function(){ var re = this; var q1 = [/x26/g,/x3C/g,/x3E/g,/x20/g]; var q2 = ["&","<",">"," "]; for(var i=0;i<q1.length;i++) re = re.replace(q1[i],q2[i]); return re;}// Unicode轉化String.prototype.ascW = function(){ var strText = ""; for (var i=0; i<this.length; i++) strText += "" + this.charCodeAt(i) + ";"; return strText;}