JavaScript如何自定义trim方法
相比vbscript,javascript在字符串处理方面已经很强大了,但是偏偏缺少去除字符串前后空格的trim方法。
//cleartherightandleftspace
functiontrim(s){
returntrimRight(trimLeft(s));
}
//cleartheleftspace
functiontrimLeft(s){
if(s==null){
return"";
}
varwhitespace=newString("\t\n\r");
varstr=newString(s);
if(whitespace.indexOf(str.charAt(0))!=-1){
varj=0,i=str.length;
while(j<i&&whitespace.indexOf(str.charAt(j))!=-1){
j++;
}
str=str.substring(j,i);
}
returnstr;
}
//cleartherightspace
functiontrimRight(s){
if(s==null)return"";
varwhitespace=newString("\t\n\r");
varstr=newString(s);
if(whitespace.indexOf(str.charAt(str.length-1))!=-1){
vari=str.length-1;
while(i>=0&&whitespace.indexOf(str.charAt(i))!=-1){
i--;
}
str=str.substring(0,i+1);
}
returnstr;
}
以上就是本文的全部内容,希望对大家的学习有所帮助。
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短