CSDN专家博客精华版

为人民服务!
  首页  :: 新随笔  :: 管理

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

Posted on 2007-12-17 10:22  csdnexpert  阅读(100)  评论(0)    收藏  举报

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