best way to remove file extension [duplicate]

非 Y 不嫁゛ 提交于 2020-01-22 13:09:05

问题


What is the shortest way to remove file extension? this is what I have tried:

file = "/home/usr/my_file.xml"
file = File.basename(file)
file.slice! File.extname(file)   #=> my_file

回答1:


Read the documentation of File::basename :

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_SEPARETOR 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 = "/home/usr/my_file.xml"
File.basename(file,File.extname(file)) # => "my_file"


来源:https://stackoverflow.com/questions/20901969/best-way-to-remove-file-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!