JS分解字符串
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <title>JS分解字符串</title>
- <script type="text/javascript">
- String.prototype.trim = function(){
- // 用正则表达式将前后空格,用空字符串替代。
- return this.replace(/(^\s*)|(\s*$)/g,"");
- }
- function ttt(){
- var str=" ,你好,gggg,999, ";
- alert("old["+str+"]");
- strstr=str.replace(",",",");
- alert("["+str+"]");
- strstr=str.trim();
- if(str.substring(0,1)==","){
- strstr=str.substring(1,str.length);
- }
- if(str.substring(str.length-1,str.length)==","){
- strstr=str.substring(0,str.length-1);
- }
- alert("["+str+"]");
- var sa=str.split(",");
- var i;
- for(i=0;i<sa.length;i++){
- alert("["+i+"] "+sa[i]);
- }
- }
- </script>
- </head>
- <body>
- <input type="button" name="Submit" onClick="ttt();" value="按钮" />
- </body>
- </html>