日期:2014-05-16 浏览次数:20365 次
<script type="text/javascript" language="javascript"> var s = " fOo bar "; for(var index = 0; index < s.length; index++){ alert(s.charCodeAt(index)); alert(s.charAt(index)); } //把左边的空格删除 function ltrim(s){ var flg = 0; var lstr = ''; while(s.charCodeAt(flg) == 32 )flg++; alert(flg); for(var index = 0; index < s.length - flg; index++) lstr+= s.charAt(flg+index); return lstr; } //把右边的空格删除 function rtrim(s){ var flg = 0; var rstr = ''; var strLength = s.length; while( s.charAt(strLength-1-flg) == ' ')flg++; for(var index = 0; index < s.length - flg; index++) rstr+= s.charAt(index); return rstr; } //利用正则表达式删除空格 function replaceSpace(str){ return str.replace(/\s/g,''); } //删除所有空格 function removeAllSpace(str){ var localString = ''; for(var index = 0; index<str.length; index++) if(str.charCodeAt(index)!= 32){ localString += str.charAt(index); }; return localString; } </script>