What is the best way to get the filename from a String or a File object, removing the extension?
getNameWithoutExtension() method. It allows us to remove the extension from the given filename easily. The implementation is pretty straightforward. If the filename contains dots, the method cuts from the last dot to the end of the filename.
I found that creating a File object is a straightforward way to achieve this. No actual file will be created on disk. But take care that only the last extension will be removed:
File("myFile.txt").nameWithoutExtension
File("myFile.tar.gz").nameWithoutExtension
result:
"myFile"
"myFile.tar"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With