java字符串截取 String path="翔安区\马巷镇\舫阳村\坪边\01_FWZDT.dwg" 我想把最后一个\后面的文件截掉,需要得到String path="翔安区\马巷镇\舫阳村\坪边\" 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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); } url得到最后一个\的位置(因为文件名是肯定没有\的)然后用substring方法截取得到想要的字符串 如果文件名固定的话可以用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); } 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("\\"); } } 有关抽象函数的问题 一个问题 如何实现读取应用程序配置文件并生成串口对象? 我想问下做一个贪吃蛇的程序要用到哪些知识,具体一点的 急!!jar命令使用问题:想在jar中连接数据库,需要class12.jar,怎么处理 jsp上传后缀问题? 子类继承父类的时候,是否也继承了父类的构造函数? applet的一个动态显示时间的问题 如何看《Thinking in java》这本书? visual age for java3.5下载 静态与非静态代码块 “蓝桥杯”决赛题4
String path="翔安区\\马巷镇\\舫阳村\\坪边\\01_FWZDT.dwg";
int index = path.lastIndexOf("\\");
System.out.println(index);
String newPath = path.substring(0,index+1);
System.out.println(newPath);
}
然后用substring方法截取得到想要的字符串
要不然就用正则表达式里的group
String path="翔安区\\马巷镇\\舫阳村\\坪边\\01_FWZDT.dwg";
int index = path.lastIndexOf("\\");
String newPath = path.substring(0,index+1);
System.out.println(newPath);
}
一楼写的不是可以吗,怎么还没结贴!
String path="翔安区\\马巷镇\\舫阳村\\坪边\\01_FWZDT.dwg";
int index = path.lastIndexOf("\\");
System.out.println(index);
String newPath = path.substring(0,index+1);
System.out.println(newPath);
}
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("\\");
}
}