日期:2014-05-20  浏览次数:20604 次

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

------解决方案--------------------
Java code
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);
    }

------解决方案--------------------
探讨

Java code
public static void main(){
String path="翔安区\\马巷镇\\舫阳村\\坪边\\01_FWZDT.dwg";
int index = path.lastIndexOf("\\");
System.out.println(index);
String newPath = pa……

------解决方案--------------------
如果文件名固定的话可以用substring
要不然就用正则表达式里的group
------解决方案--------------------
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);
}
一楼写的不是可以吗,怎么还没结贴!
------解决方案--------------------
的确 ,一楼的方法就可以 ,String.lastIndexOf("\\") ;
------解决方案--------------------
嗯 一楼的写的不错呢!!
------解决方案--------------------
嗯,一楼的方法可行。
------解决方案--------------------
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);
}