把字符串中不规则的空格取掉或换成"/"

来源:互联网 发布:dw软件下载 编辑:程序博客网 时间:2024/05/22 14:05

 当一个STRING中有不规则的空格时,我门要去掉空格使里边字符连接在一起(或用别的字符代替)

package cloes;
//import java.lang.*;
/**
 *
 * @author guo
 */
public class Cloes {
   
    /** Creates a new instance of Cloes */
    public Cloes() {
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int j=0;
        int z=0;
        boolean b=false;
        String s1;
        StringBuffer sb=new StringBuffer(500);
       String s="abc  d     efd h      ijk  l   mn   o  pq  r s       t  u     v   w   x  y  z";
        System.out.println(s.length());
        //System.out.println(s.charAt(2));
        for(int i=0;i<s.length()-1;i++)
        {
           
            char c=s.charAt(i);
           // System.out.println("当前字符"+c);                         ///////////////////
            if(c!=' ')
            {
           //     System.out.println("c!=' '");              ///////////////////
                if(j!=-1)
                {
                  //  System.out.println("J的值"+j);                 //////////////////
                    if(b) j=i;
                    b=false;
                    System.out.println(j);
                    System.out.println(b);
                    if(s.charAt(i+1)==' ')
                    {
                        if(i!=s.length()-2)
                        {
                   //        System.out.println("进来了!");   ////////////////////////
                           z=i;
                           s1=s.substring(j,z+1);
                          // System.out.println(z);               ////////////////////
                        //   System.out.println(j);               ////////////////
                           sb.append(s1);
                           sb.append("/");
                           System.out.println(i);
                        }

                    }
                      if(i==s.length()-2)
                        {
                          
                            z=i+1;
                            s1=s.substring(j,z+1);
                           // System.out.println(j+"   "+z);
                            sb.append(s1);
                           
                        }
                }
            }
            if(c==' ')
            {
               j=-1;
               System.out.println("c==' '"+j);
               if(s.charAt(i+1)!=' ')
               {
               j=i;
               b=true;
               }
               if(i==s.length()-2)
               {
                   j=i+1;
                   z=i+1;
                   s1=s.substring(j,z+1);
                   sb.append(s1);
               }
            }
       
    } 
    System.out.println(sb.toString());
   // System.out.println(s.substring(,9));
    }
}

输出结果

abc/d/efd/h/ijk/l/mn/o/pq/r/s/t/u/v/w/x/y/z

原创粉丝点击