DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> C#中TrimStart,TrimEnd,Trim在javascript上的實現
C#中TrimStart,TrimEnd,Trim在javascript上的實現
編輯:關於JavaScript     
於是乎,自己動手寫了個!!看到很多人都是用正則,咱不會,就用了最土的方法來實現了!帖上代碼吧!希望對大家有所幫助!!!
復制代碼 代碼如下:
String.prototype.trimStart = function(trimStr){
if(!trimStr){return this;}
var temp = this;
while(true){
if(temp.substr(0,trimStr.length)!=trimStr){
break;
}
temp = temp.substr(trimStr.length);
}
return temp;
};
String.prototype.trimEnd = function(trimStr){
if(!trimStr){return this;}
var temp = this;
while(true){
if(temp.substr(temp.length-trimStr.length,trimStr.length)!=trimStr){
break;
}
temp = temp.substr(0,temp.length-trimStr.length);
}
return temp;
};
String.prototype.trim = function(trimStr){
var temp = trimStr;
if(!trimStr){temp=" ";}
return this.trimStart(temp).trimEnd(temp);
};

用法大家應該明了吧!!!這裡就不說了哈!!!有問題請指明!謝謝!
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved