How do I trim a file extension from a String in Java?
修剪Java后缀的最有效方法是什么,如下所示:
1 2 3 4 5 | title part1.txt title part2.html => title part1 title part2 |
这是我们不应该自己做的代码。将库用于平凡的东西,为硬的东西而动脑子。
在这种情况下,我建议使用Apache Commons IO的FilenameUtils.removeExtension()
1 | str.substring(0, str.lastIndexOf('.')) |
由于在单行中使用
以以下路径为例:
1 | a.b/c |
使用单线将导致:
1 | a |
不对
结果应该是
需要检查
受skaffman答案的启发,我看了Apache Commons IO的
为了重新创建其行为,我编写了新方法应满足的一些测试,这些测试如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 | Path Filename -------------- -------- a/b/c c a/b/c.jpg c a/b/c.jpg.jpg c.jpg a.b/c c a.b/c.jpg c a.b/c.jpg.jpg c.jpg c c c.jpg c c.jpg.jpg c.jpg |
(这就是我检查过的全部内容-可能还有其他应被我忽略的检查。)
实施
以下是我对
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public static String removeExtension(String s) { String separator = System.getProperty("file.separator"); String filename; // Remove the path upto the filename. int lastSeparatorIndex = s.lastIndexOf(separator); if (lastSeparatorIndex == -1) { filename = s; } else { filename = s.substring(lastSeparatorIndex + 1); } // Remove the extension. int extensionIndex = filename.lastIndexOf("."); if (extensionIndex == -1) return filename; return filename.substring(0, extensionIndex); } |
通过上面的测试运行
使用以下代码测试了该方法。在Windows上运行时,路径分隔符是
1 2 3 4 5 6 7 8 9 10 11 | System.out.println(removeExtension("a\\b\\c")); System.out.println(removeExtension("a\\b\\c.jpg")); System.out.println(removeExtension("a\\b\\c.jpg.jpg")); System.out.println(removeExtension("a.b\\c")); System.out.println(removeExtension("a.b\\c.jpg")); System.out.println(removeExtension("a.b\\c.jpg.jpg")); System.out.println(removeExtension("c")); System.out.println(removeExtension("c.jpg")); System.out.println(removeExtension("c.jpg.jpg")); |
结果是:
1 2 3 4 5 6 7 8 9 | c c c.jpg c c c.jpg c c c.jpg |
结果是该方法应满足的测试中概述的期望结果。
顺便说一句,就我而言,当我想要快速解决方案以删除特定扩展名时,这大致就是我所做的:
1 2 3 4 | if (filename.endsWith(ext)) return filename.substring(0,filename.length() - ext.length()); else return filename; |
1 2 |
如果您的项目已经依赖于Google核心库,请使用
1 2 3 4 5 | String fileName="foo.bar"; int dotIndex=fileName.lastIndexOf('.'); if(dotIndex>=0) { // to prevent exception if there is no dot fileName=fileName.substring(0,dotIndex); } |
这是一个技巧问题吗? :p
我想不出更快的atm方法。
1 | filename.substring(filename.lastIndexOf('.'), filename.length()).toLowerCase(); |
我发现coolbird的答案特别有用。
但是我将最后一个结果语句更改为:
1 2 3 4 5 | if (extensionIndex == -1) return s; return s.substring(0, lastSeparatorIndex+1) + filename.substring(0, extensionIndex); |
因为我希望返回完整的路径名。
1 2 3 | So"C:\Users\mroh004.COM\Documents\Test\Test.xml" becomes "C:\Users\mroh004.COM\Documents\Test\Test" and not "Test" |
您可以尝试此功能,非常基本
1 2 3 |
使用正则表达式。这个替换最后一个点,以及后面的所有内容。
1 |
如果要预编译正则表达式,也可以创建Pattern对象。
用字符串图像路径创建一个新文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | String imagePath; File test = new File(imagePath); test.getName(); test.getPath(); getExtension(test.getName()); public static String getExtension(String uri) { if (uri == null) { return null; } int dot = uri.lastIndexOf("."); if (dot >= 0) { return uri.substring(dot); } else { // No extension. return""; } } |
org.apache.commons.io.FilenameUtils版本2.4提供以下答案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | public static String removeExtension(String filename) { if (filename == null) { return null; } int index = indexOfExtension(filename); if (index == -1) { return filename; } else { return filename.substring(0, index); } } public static int indexOfExtension(String filename) { if (filename == null) { return -1; } int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR); int lastSeparator = indexOfLastSeparator(filename); return lastSeparator > extensionPos ? -1 : extensionPos; } public static int indexOfLastSeparator(String filename) { if (filename == null) { return -1; } int lastUnixPos = filename.lastIndexOf(UNIX_SEPARATOR); int lastWindowsPos = filename.lastIndexOf(WINDOWS_SEPARATOR); return Math.max(lastUnixPos, lastWindowsPos); } public static final char EXTENSION_SEPARATOR = '.'; private static final char UNIX_SEPARATOR = '/'; private static final char WINDOWS_SEPARATOR = '\'; |
1 2 3 4 5 |
1 2 |
1 2 3 4 5 6 7 8 |
请记住没有文件扩展名或存在多个文件扩展名的情况
示例文件名: file.txt | file.tar.bz2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** * * @param fileName * @return file extension * example file.fastq.gz => fastq.gz */ private String extractFileExtension(String fileName) { String type ="undefined"; if (FilenameUtils.indexOfExtension(fileName) != -1) { String fileBaseName = FilenameUtils.getBaseName(fileName); int indexOfExtension = -1; while (fileBaseName.contains(".")) { indexOfExtension = FilenameUtils.indexOfExtension(fileBaseName); fileBaseName = FilenameUtils.getBaseName(fileBaseName); } type = fileName.substring(indexOfExtension + 1, fileName.length()); } return type; } |
我会这样:
1 2 3 4 | String title_part ="title part1.txt"; int i; for(i=title_part.length()-1 ; i>=0 && title_part.charAt(i)!='.' ; i--); title_part = title_part.substring(0,i); |
从头到尾直到。然后调用子字符串。
编辑:
可能不是高尔夫,但是很有效:)
1 2 3 4 5 6 7 8 9 10 11 |
这将输出img和imgLink的示例