欢迎访问:常州市武进区嘉泽中心小学网站 !今天是:
栏目列表
您现在的位置是:首页>>教师>>计算机技术>>网站制作技术>>文章内容
javascript:去除字符串的前后空格
发布时间:2008-11-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“


附件:
    关闭窗口
    打印文档
    账号登录
    保持登录 忘记密码?
    账号与武进教师培训平台同步