Ruby:获取没有扩展名的文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23356777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby: Get filename without the extensions
提问by konyak
How can I get the filename without the extensions? For example, "test.html.erb" should be "test"
如何获取没有扩展名的文件名?例如,“test.html.erb”应该是“test”
Similar question. However,
类似的问题。然而,
> File.basename("test.html.erb", ".*")
=> "test.html"
EDIT: The above code is my failed attempt to produce the desired result. I understand I am using File.basename incorrectly. Thus, I posted this question. When I say filename without the extensions, I mean I don't care about the stuff after the first dot. The name that I'm trying to return will not have a dot. Also, in actual code I will passing in __FILE__instead of "test.html.erb".
编辑:上面的代码是我尝试产生所需结果的失败。我知道我错误地使用了 File.basename。因此,我发布了这个问题。当我说没有扩展名的文件名时,我的意思是我不关心第一个点之后的内容。我试图返回的名称不会有一个点。此外,在实际代码中,我将传入__FILE__而不是"test.html.erb".
回答by Зелёный
Read documentation:
阅读文档:
basename(file_name [, suffix] ) → base_name
basename(file_name [, suffix] ) → base_name
Returns the last component of the filename given in file_name, which can be formed using both File::SEPARATOR and File::ALT_SEPARATOR as the separator when File::ALT_SEPARATOR is not nil. If suffix is given and present at the end of file_name, it is removed.
返回 file_name 中给出的文件名的最后一部分,当 File::ALT_SEPARATOR 不为零时,可以使用 File::SEPARATOR 和 File::ALT_SEPARATOR 作为分隔符形成文件名。如果给定后缀并出现在 file_name 的末尾,则将其删除。
=> File.basename('public/500.html', '.html')
=> "500"
in you case:
在你的情况下:
=> File.basename("test.html.erb", ".html.erb")
=> "test"
回答by fguillen
In case you don't know the extension you can combine File.basenamewith File.extname:
如果您不知道扩展名,您可以结合File.basename使用File.extname:
filepath = "dir/dir/filename.extension"
File.basename(filepath, File.extname(filepath)) #=> "filename"
回答by andrewdotn
Pathnameprovides a convenient object-oriented interface for dealing with file names.
Pathname提供了一个方便的面向对象的接口来处理文件名。
One method lets you replace the existing extension with a new one, and that method accepts the empty string as an argument:
一种方法允许您用新扩展替换现有扩展,该方法接受空字符串作为参数:
>> Pathname('foo.bar').sub_ext ''
=> #<Pathname:foo>
>> Pathname('foo.bar.baz').sub_ext ''
=> #<Pathname:foo.bar>
>> Pathname('foo').sub_ext ''
=> #<Pathname:foo>
This is a convenient way to get the filename stripped of its extension, if there is one.
这是一种将文件名从其扩展名中剥离的便捷方法(如果有的话)。
But if you want to get rid of all extensions, you can use a regex:
但是如果你想摆脱所有的扩展,你可以使用正则表达式:
>> "foo.bar.baz".sub(/\..*/, '')
=> "foo"
回答by don.najd
How about this
这个怎么样
File.basename(f, File.extname(f))
File.basename(f, File.extname(f))
returns the file name without the extension.. works for filenames with multiple '.' in it.
返回不带扩展名的文件名.. 适用于带有多个“.”的文件名 在里面。
回答by xdazz
Split by dot and the first part is what you want.
按点分割,第一部分就是你想要的。
filename = 'test.html.erb'
result = filename.split('.')[0]
回答by user3387633
Considering the premise, the most appropriate answer for this case (and similar cases with other extensions) would be something such as this:
考虑到前提,对于这种情况(以及其他扩展的类似情况)最合适的答案是这样的:
__FILE__.split('.')[0...-1].join('.')
Which will only remove the extension (not the other parts of the name: myfile.html.erbhere becomes myfile.html, rather than just myfile.
这只会删除扩展名(而不是名称的其他部分:myfile.html.erb这里变为myfile.html,而不仅仅是myfile.
回答by konyak
Thanks to @xdazz and @Monk_Code for their ideas. In case others are looking, the final code I'm using is:
感谢@xdazz 和@Monk_Code 的想法。如果其他人正在寻找,我使用的最终代码是:
File.basename(__FILE__, ".*").split('.')[0]
Assuming __FILE__is used and there is no period in the resulting string, this generically allows you to remove the path and extensions in front and back of the file, respectively.
假设__FILE__使用了并且结果字符串中没有句点,这通常允许您分别删除文件前后的路径和扩展名。
回答by iam habbeboy
name = "filename.100.jpg"
puts "#{name.split('.')[-1]}"

