By Ben
方法一:
while((x.length>0) && (x.charAt(0)==' '))
x = x.substring(1,x.length);
//while((x.length>0) && (x.charAt(x.length-1)==' '))
while(x.length>0)
x = x.substring(0,x.length-1);
alert("Kill=" + x + "==")
return x;
}
实例: var a = KillSpace(“ abc “); //得出a == “abc“
方法二:
String.prototype.trim = function()
{
return this.replace(/(^/s+)|/s+$/g,"");
}
实例: var a = “ abc “.trim(); //得出a == “abc“
本文介绍了两种在JavaScript中去除字符串首尾空格的方法:一种是通过循环逐步删除空格,另一种则是利用正则表达式进行替换。这些方法有助于开发者在处理用户输入等场景时保证数据的整洁。
833

被折叠的 条评论
为什么被折叠?



