String.prototype.trim = function() {
return (this.replace(/^\s+|\s+$/g,""));
}alert("   cloudchen   ".trim().bold())

解决方案 »

  1.   

    function trim(str){
    return str.replace(/^[\s]+|[\s]+$/,"");
    }
    document.write(trim("       fdsdfs     dfsdfs     "));
      

  2.   

    function trim(str)
    { var i;
    i=0;
    while (i<str.length && str.charAt(i)==' ') 
    i++;
    str=str.substr(i);
    i=str.length-1;
    while (i>=0 && str.charAt(i)==' ')
     i--;
     str=str.substr(0,i+1);
     return str;}
      

  3.   

    /************************************
    function: trim(strInput)
    purpose: 去除字符串的前后空格
    parameters : strInput: 字符串
    return value : 去除前后空格的字符串
    ************************************/
    function  trim(strInput)
    {
    strInput=new String(strInput)
    var iLoop=0;
    var iLoop2=-1;
    var strChr;
    if((strInput == null)||(strInput == "<NULL>"))
    return "";
    if(strInput)
    {
    for(iLoop=0;iLoop<strInput.length-1;iLoop++)
    {
    strChr=strInput.charAt(iLoop);
    if(strChr!=' ')
    break;
    }
    for(iLoop2=strInput.length-1;iLoop2>=0;iLoop2--)
    {
    strChr=strInput.charAt(iLoop2);
    if(strChr!=' ')
    break;
    }
    }

    if(iLoop<=iLoop2)
    {
    return strInput.substring(iLoop,iLoop2+1);
    }
    else
    {
    return "";
    }
    }
      

  4.   

    function trim(Str)
    {
       Str=Str.replae(/^ /g,"");
       Str=Str.replae(/ $/g,"");
       return Str;
    }
      

  5.   

    <script language="JavaScript">
    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)==" ")) x=x.substring(0,x.length-1);
    return x;
    }
      

  6.   

    最简单的办法……<script language=VBscript runat=Server>
    function vbTrim(str)
        vbTrim=trim(str)
    end function
    </script>然后Js可以直接调用
      

  7.   

    purexu(星星) 的发言调试成功了,而且比较直观