groovy是否有一个简单的方法来获得没有扩展名的文件名? [英] Does groovy have an easy way to get a filename without the extension?

查看:209
本文介绍了groovy是否有一个简单的方法来获得没有扩展名的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这样的事情:

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

每个文件在 test 目录中。有Groovy的方式来获得没有任何扩展名的文件名? (或者我回到正则表达式的土地?)

解决方案

我相信最grooviest的方式是:

  file.name.lastIndexOf('。')。with {it!= -1? file.name [0 ..< it]:file.name} 

或者用简单的正则表达式:

$ p $ file.name.replaceFirst(〜/ \。[^ \。] + $ /,'')

还有一个apache commons-io java lib用于这种用途,如果你愿意,使用maven:

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


Say I have something like this:

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

This prints the full filename of every file in the test directory. Is there a Groovy way to get the filename without any extension? (Or am I back in regex land?)

解决方案

I believe the grooviest way would be:

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

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)

这篇关于groovy是否有一个简单的方法来获得没有扩展名的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆