Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the file extension from a url?

Tags:

ruby

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

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

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

string.format("http://www.example.com/{0}.{1}", filename, extension); 
like image 242
Blankman Avatar asked Nov 06 '10 15:11

Blankman


People also ask

Is there any way to get the file extension from a URL?

String extension = uri. substring(url. lastIndexOf(".") + 1);

What is a URL file extension?

A URL file is a shortcut that points to a specific Uniform Resource Locator. When you double-click a URL file, your computer accesses the URL the file contains. URL files most often contain https: web addresses and are used to access web pages. However, URL files can also contain mailto:, tel:, file:, or other URLs.


1 Answers

Use File.extname

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

To format the string

"http://www.example.com/%s.%s" % [filename, extension] 
like image 146
Simone Carletti Avatar answered Sep 20 '22 16:09

Simone Carletti