Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy way to remove file extension? [duplicate]

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)
like image 313
Jay Prall Avatar asked Dec 16 '15 20:12

Jay Prall


1 Answers

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

like image 103
tim_yates Avatar answered Sep 19 '22 04:09

tim_yates