String字符串去掉最后一个","号的几种方式

      String a = "struts-default.xml,struts-plugin.xml,struts.xml";
        String[] bStrings = a.split(","); //以','号来分割字符串
        
        System.out.println(bStrings[0]);
        
        int blength = bStrings.length;        
        System.out.println(blength);
        
        for(int i=0;i<bStrings.length;i++){
            System.out.println(bStrings[i]);
        }
        
        String b = "struts-default.xml,struts-plugin.xml,struts.xml,";
        System.out.println(b.substring(0, b.length()-1)); //去掉最后一个','号
        //这种方式比较灵活
        int index = a.lastIndexOf(","); //去掉中间的','号
        System.out.println(a.substring(0, index)+a.substring(index+1, a.length()));
        
        int indexb = b.lastIndexOf(","); //去掉最后一个','号
        System.out.println(b.substring(0, indexb)+b.substring(indexb+1, b.length()));

 

posted @ 2016-11-14 16:22  super超人  阅读(28698)  评论(0编辑  收藏  举报