String path="翔安区\马巷镇\舫阳村\坪边\01_FWZDT.dwg" 我想把最后一个\后面的文件截掉,需要得到String path="翔安区\马巷镇\舫阳村\坪边\" 

解决方案 »

  1.   

    public static void main(){
    String path="翔安区\\马巷镇\\舫阳村\\坪边\\01_FWZDT.dwg";
    int index = path.lastIndexOf("\\");
    System.out.println(index);
    String newPath = path.substring(0,index+1);
    System.out.println(newPath);
    }
      

  2.   

    url得到最后一个\的位置(因为文件名是肯定没有\的)
    然后用substring方法截取得到想要的字符串
      

  3.   

    如果文件名固定的话可以用substring
    要不然就用正则表达式里的group
      

  4.   

    public static void main(String[] args){
            String path="翔安区\\马巷镇\\舫阳村\\坪边\\01_FWZDT.dwg";
            int index = path.lastIndexOf("\\");
            String newPath = path.substring(0,index+1);
            System.out.println(newPath);
        }
    一楼写的不是可以吗,怎么还没结贴!
      

  5.   

    的确 ,一楼的方法就可以 ,String.lastIndexOf("\\") ;
      

  6.   

    public static void main(){
            String path="翔安区\\马巷镇\\舫阳村\\坪边\\01_FWZDT.dwg";
            int index = path.lastIndexOf("\\");
            System.out.println(index);
            String newPath = path.substring(0,index+1);
            System.out.println(newPath);
        }
      

  7.   


                    String[] s=path.split("\\\\");
    StringBuffer sb=new StringBuffer();
    for(int i=0;i<s.length-1;i++){
    if(i==(s.length-2)){
    sb.append(s[i]);
    }else{
    sb.append(s[i]);
    sb.append("\\");
    }
    }