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.
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
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)
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With