Does groovy have an easy way to get a filename without the extension?

前端 未结 10 1087
-上瘾入骨i
-上瘾入骨i 2020-12-14 14:41

Say I have something like this:

new File(\"test\").eachFile() { file->  
println file.getName()  
}

This prints the full filename of eve

10条回答
  •  醉酒成梦
    2020-12-14 15:11

    I believe the grooviest way would be:

    file.name.lastIndexOf('.').with {it != -1 ? file.name[0..

    or with a simple regexp:

    file.name.replaceFirst(~/\.[^\.]+$/, '')
    

    also there's an apache commons-io java lib for that kinda purposes, which you could easily depend on if you use maven:

    org.apache.commons.io.FilenameUtils.getBaseName(file.name)
    

提交回复
热议问题