I'm wondering if there is a "Groovy" way to remove the file extension from a filename.
The current solution relies on the apache commons io package:
import org.apache.commons.io.FilenameUtils
String filename = '/tmp/hello-world.txt'
def fileWithoutExt = FilenameUtils.removeExtension(filename)
You can do something like this:
filename[0..<filename.lastIndexOf('.')]
To remove everything after the last .
in the String.
Or the slightly prettier:
filename.take(filename.lastIndexOf('.'))
NB: if a file haven't an extension it will be not matched
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