Codewars3

 1 public class FileNameExtractor {
 2     public static String extractFileName(String dirtyFileName) {
 3 
 4         //获得所给路径文件名
 5 //        String fName = dirtyFileName.trim();  
 6 //        String temp[] = fName.split("\\\\"); /**split里面必须是正则表达式,"\\"的作用是对字符串转义*/  
 7 //       String fileName = temp[temp.length-1];  
 8 //        return fileName;
 9         
10         String caselsh = dirtyFileName.split("\\_")[0];
11         File f = new File(dirtyFileName);
12         String fileName=f.getName();
13         System.out.println(fileName);
14         String prefix=fileName.substring(fileName.lastIndexOf("."));//如果想获得不带点的后缀,变为fileName.lastIndexOf(".")+1
15         int num=prefix.length();//得到后缀名长度
16         String fileOtherName=fileName.substring(caselsh.length()+1, fileName.length()-num);//得到文件名。去掉了后缀和前缀
17         return fileOtherName;
18     }
19 
20     public static void main(String[] args) {    
21         String string="1231231223123131_FILE_NAME.EXTENSION.OTHEREXTENSION";
22         System.out.println(extractFileName(string));
23     }
24 }
View Code

猜你喜欢

转载自www.cnblogs.com/non-variable/p/12381938.html