Maison > interface Web > js tutoriel > le corps du texte

C#中TrimStart,TrimEnd,Trim在javascript上的实现_javascript技巧

WBOY
Libérer: 2016-05-16 18:11:43
original
1104 Les gens l'ont consulté

于是乎,自己动手写了个!!看到很多人都是用正则,咱不会,就用了最土的方法来实现了!帖上代码吧!希望对大家有所帮助!!!

复制代码 代码如下:

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);
};

用法大家应该明了吧!!!这里就不说了哈!!!有问题请指明!谢谢!
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!