当前位置: 代码迷 >> JavaScript >> JS左往空格、右去空格和全去空格
  详细解决方案

JS左往空格、右去空格和全去空格

热度:391   发布时间:2012-06-29 15:48:47.0
JS左去空格、右去空格和全去空格
<script>
        function trim(str){
                if(str.length < 1){
                        return "";
                }
                str = rtrim(str);
                str = ltrim(str);
                if(str==""){
                        return "";
                }else{
                        return str;
                }
        } 

        function rtrim(str){
                var w_space = String.fromCharCode(32);
                var v_length = str.length;
                var strTemp = "";
                if(v_length < 0){
                        return "";
                }
                var iTemp = v_length -1;

                while(iTemp > -1){
                        if(str.charAt(iTemp) == w_space){
                        }else{
                                strTemp = str.substring(0,iTemp +1);
                                break;
                        }
                        iTemp = iTemp-1;

                } 
                return strTemp;
        } 

        function ltrim(str){
                var w_space = String.fromCharCode(32);
                if(v_length < 1){
                        return"";
                }
                var v_length = str.length;
                var strTemp = "";
                
                var iTemp = 0;
                
                while(iTemp < v_length){
                        if(str.charAt(iTemp) == w_space){
                        }else{
                                strTemp = str.substring(iTemp,v_length);
                                break;
                        }
                        iTemp = iTemp + 1;
                } 
                return strTemp;
        }

        function check(){
                document.login.LoginName.value = trim(document.login.LoginName.value)
                return true;
        }

</script>
  相关解决方案