首页 > 代码库 > javascript 压缩空格代码演示

javascript 压缩空格代码演示

                                                                 压缩空格代码演示

 

主要是讲解 压缩一个字符串两段空格 

        例如:javascript函数里的空格不论是这样     var s = "Hello World        "; 

                                这样        var s = "       Hello World"; 

                                 这样       var s = "     Hello World       "; 

                                 还是这样            var s = "  Hello World                "; 

          为了更加明显观察字符串后面空格效果

                        我们在取出结果的时候这样写  alert(Trim(s)+".....")     给函数Trim(s)加上了.....

                        经过压缩空格之后都是这样效果

                                                

                                           技术分享

                                                 

 代码如下:

<title>无标题文档</title></head><body></body></html><script language="javascript">//压缩字符串中的两端空格。 function   Trim(m){     while((m.length>0)&&(m.charAt(0)== ))     m   =   m.substring(1, m.length);     while((m.length>0)&&(m.charAt(m.length-1)== ))     m = m.substring(0, m.length-1);     return m;    } var s = "Hello World     ";alert(Trim(s)+".....")</script>

 

javascript 压缩空格代码演示