如何在 Java 中从字符串中删除文件扩展名?
在Java中修剪后缀最有效的方法是什么,如下所示:
title part1.txt
title part2.html
=>
title part1
title part2
What's the most efficient way to trim the suffix in Java, like this:
title part1.txt
title part2.html
=>
title part1
title part2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(23)
伙计们,不要给心灵带来压力。 我已经做过很多次了。 只需将此公共静态方法复制粘贴到您的 staticUtils 库中以供将来使用;-)
dont do stress on mind guys. i did already many times. just copy paste this public static method in your staticUtils library for future uses ;-)
我会这样做:
从开始到结束直到“。” 然后调用子串。
编辑:
可能不是高尔夫,但它很有效:)
I would do like this:
Starting to the end till the '.' then call substring.
Edit:
Might not be a golf but it's effective :)
请记住没有文件扩展名或有多个文件扩展名的情况
示例文件名:文件| 文件.txt | 文件.tar.bz2
Keeping in mind the scenarios where there is no file extension or there is more than one file extension
example Filename : file | file.txt | file.tar.bz2
这将为 img 和 imgLink 输出 example
This will output example for both img and imgLink
此代码会将文件名分成包含“.”的部分,例如 如果文件名是file-name.hello.txt,那么它将被分解为字符串数组,{“file-name”,“hello”,“txt”}。 所以无论如何,这个字符串数组中的最后一个元素将是该特定文件的文件扩展名,因此我们可以简单地使用
arrayname.length - 1
找到任何数组的最后一个元素,所以在我们了解之后最后一个元素,我们可以将文件扩展名替换为该文件名中的空字符串。 最后将返回 file-name.hello。 ,如果您还想删除最后一个句点,则可以将仅包含句点的字符串添加到返回行中字符串数组的最后一个元素。 看起来应该是这样的,This code will spilt the file name into parts where ever it has " . ", For eg. If the file name is file-name.hello.txt then it will be spilted into string array as , { "file-name", "hello", "txt" }. So anyhow the last element in this string array will be the file extension of that particular file , so we can simply find the last element of any arrays with
arrayname.length - 1
, so after we get to know the last element, we can just replace the file extension with an empty string in that file name. Finally this will return file-name.hello. , if you want to remove also the last period then you can add the string with only period to the last element of string array in the return line. Which should look like,这是我们不应该自己编写的代码。 使用图书馆来处理平凡的事情,节省你的大脑来处理困难的事情。
在这种情况下,我建议使用 FilenameUtils.removeExtension() 来自 Apache Commons IO< /a>
This is the sort of code that we shouldn't be doing ourselves. Use libraries for the mundane stuff, save your brain for the hard stuff.
In this case, I recommend using FilenameUtils.removeExtension() from Apache Commons IO
虽然在一行中使用
String.substring
和String.lastIndex
很好,但在处理某些文件路径方面存在一些问题。以以下路径为例:
使用单行将导致:
这是不正确的。
结果应该是
c
,但由于文件缺少扩展名,但路径中有一个名称中带有.
的目录,单行方法被欺骗了将部分路径作为文件名,这是不正确的。需要检查
灵感来自skaffman的回答,我看了一下Apache Commons IO。
为了重新创建它的行为,我编写了一些新方法应该满足的测试,如下所示:(
这就是我检查过的所有内容 - 可能还有其他检查应该到位,但我忽略了。 )
实现
以下是我对
removeExtension
方法的实现:通过上述测试运行此
removeExtension
方法会产生上面列出的结果。使用以下代码对该方法进行了测试。 由于这是在 Windows 上运行的,因此路径分隔符是
\
,当用作String
文字的一部分时,必须使用\
进行转义。结果是:
结果是方法应满足的测试中概述的期望结果。
As using the
String.substring
andString.lastIndex
in a one-liner is good, there are some issues in terms of being able to cope with certain file paths.Take for example the following path:
Using the one-liner will result in:
That's incorrect.
The result should have been
c
, but since the file lacked an extension, but the path had a directory with a.
in the name, the one-liner method was tricked into giving part of the path as the filename, which is not correct.Need for checks
Inspired by skaffman's answer, I took a look at the
FilenameUtils.removeExtension
method of the Apache Commons IO.In order to recreate its behavior, I wrote a few tests the new method should fulfill, which are the following:
(And that's all I've checked for -- there probably are other checks that should be in place that I've overlooked.)
The implementation
The following is my implementation for the
removeExtension
method:Running this
removeExtension
method with the above tests yield the results listed above.The method was tested with the following code. As this was run on Windows, the path separator is a
\
which must be escaped with a\
when used as part of aString
literal.The results were:
The results are the desired results outlined in the test the method should fulfill.
顺便说一句,就我而言,当我想要一个快速解决方案来删除特定扩展时,这大约是我所做的:
BTW, in my case, when I wanted a quick solution to remove a specific extension, this is approximately what I did:
如果您的项目已经依赖于 Google 核心库,请使用
com.google.common.io.Files
类中的方法。 您需要的方法是getNameWithoutExtension
。Use a method in
com.google.common.io.Files
class if your project is already dependent on Google core library. The method you need isgetNameWithoutExtension
.你可以试试这个功能,很基础
you can try this function , very basic
这是一个技巧问题吗? :p
我想不出更快的 ATM 方式。
Is this a trick question? :p
I can't think of a faster way atm.
我找到了 coolbird 的答案特别有用。
但我将最后一个结果语句更改为:
因为我希望返回完整路径名。
I found coolbird's answer particularly useful.
But I changed the last result statements to:
as I wanted the full path name to be returned.
使用正则表达式。 这个点取代了最后一个点,以及它后面的所有内容。
如果您想预编译正则表达式,您还可以创建一个 Pattern 对象。
Use a regex. This one replaces the last dot, and everything after it.
You can also create a Pattern object if you want to precompile the regex.
如果你使用 Spring 你可以使用
If you use Spring you could use
使用字符串图像路径创建一个新文件
create a new file with string image path
org.apache.commons.io.FilenameUtils 版本 2.4 给出了以下答案
org.apache.commons.io.FilenameUtils version 2.4 gives the following answer
我能写的最好的尝试坚持 Path 类:
The best what I can write trying to stick to the Path class: