如何从 url 获取文件扩展名? [英] How to get the file extension from a url?

查看:75
本文介绍了如何从 url 获取文件扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚接触 ruby​​,我如何从如下网址获取文件扩展名:

New to ruby, how would I get the file extension from a url like:

http://www.example.com/asdf123.gif

另外,我将如何格式化这个字符串,在 c# 中我会这样做:

Also, how would I format this string, in c# I would do:

string.format("http://www.example.com/{0}.{1}", filename, extension);

推荐答案

使用 文件.extname

File.extname("test.rb")         #=> ".rb"
File.extname("a/b/d/test.rb")   #=> ".rb"
File.extname("test")            #=> ""
File.extname(".profile")        #=> ""

格式化字符串

"http://www.example.com/%s.%s" % [filename, extension]

这篇关于如何从 url 获取文件扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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