javascript 去空格

来源:互联网 发布:p2p网络管理软件 编辑:程序博客网 时间:2024/04/30 12:20

在网上收集的的东西,记录下: 

 方法一:
  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“

原创粉丝点击