Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift get file extension from web link [duplicate]

Tags:

swift

I have web link, that is something like:

http://hr-platform.nv5.pw/image/comp_1/pdf-test.pdf

It may vary in text or extension. What i want is, get "pdf" string and woule be nice to have name of file, which is in that case - "pdf-test".

How to get those strings from web link? Thanks.

like image 367
Evgeniy Kleban Avatar asked Sep 07 '17 06:09

Evgeniy Kleban


3 Answers

You can do it by using NSString with pathExtension for the file extension:

let url: NSString = "http://hr-platform.nv5.pw/image/comp_1/pdf-test.pdf" // http://hr-platform.nv5.pw/image/comp_1/pdf-test.pdf
let fileExtension = url.pathExtension // pdf
let urlWithoutExtension = url.deletingPathExtension // http://hr-platform.nv5.pw/image/comp_1/pdf-test

As @David Berry suggested use the URL-class:

let url = URL(string: "http://hr-platform.nv5.pw/image/comp_1/pdf-test.pdf")
let fileExtension = url?.pathExtension // pdf
let fileName = url?.lastPathComponent // pdf-test.pdf
like image 70
Rashwan L Avatar answered Nov 16 '22 07:11

Rashwan L


Use the URL class:

let url = URL(string: "http://hr-platform.nv5.pw/image/comp_1/pdf-test.pdf")
print(url?.pathExtension)
print(url?.deletingPathExtension().lastPathComponent)
like image 41
David Berry Avatar answered Nov 16 '22 07:11

David Berry


You can do this:

let urlStr = "http://hr-platform.nv5.pw/image/comp_1/pdf-test.pdf"

var  componentsArr = urlStr.components(separatedBy: "/")
if let fileName = componentsArr.last {
    print(fileName)
}
like image 24
3stud1ant3 Avatar answered Nov 16 '22 09:11

3stud1ant3