您的位置:首页 > Web前端 > JavaScript

javascript:去除字符串的前后空格

2007-12-23 14:05 501 查看
By Ben

方法一:
function KillSpace(x){
  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“

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=131484
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: