0

I have the following string type:

file:///private/var/mobile/Containers/Data/Application/1C02189F-2426-4420-A178-1CC420CBBD3A/tmp/com.test.Test-Inbox/2015-04-23_00002.jpg

And I need to access the file extension which in this case is .jpg

I want to access the string after the last . since it will always be the file extension I want, but I cannot substring it to the last 3 chars because the extension might have 4 chars like .docx or .mpeg

What would be the best way to do this?

I thought in reverse the string and loop char by char to get the latest (in this case first) . and then as I find the length I could substring it.

Any thoughts?

Thanks

Ivan Cantarino
  • 3,058
  • 4
  • 34
  • 73
  • see this https://stackoverflow.com/questions/17828044/is-it-possible-to-find-the-file-extension-of-a-uiimage – Anbu.Karthik Jul 26 '17 at 12:57
  • that case is specific for images, and in my case the file could be of any type, and I get a pointer URL instead of a raw NSData type. thanks for pointing that out – Ivan Cantarino Jul 26 '17 at 12:59

5 Answers5

2

In Swift3, You can easily get from path only. No need of doing any extra stuff.

let filePath:NSString = "the_file_name.png"
let fileExtension = filePath.pathExtension
Sivajee Battina
  • 4,124
  • 2
  • 22
  • 45
1

You can use String.components(separatedBy:) to break the String into substrings separated by a certain character ("." in your case). Since you know the file extension will be after the last separator, you can just call .last on the array of substrings to access it.

let fileNameWithExtension = "file:///private/var/mobile/Containers/Data/Application/1C02189F-2426-4420-A178-1CC420CBBD3A/tmp/com.test.Test-Inbox/2015-04-23_00002.jpg"
let fileExtension = fileNameWithExtension.components(separatedBy: ".").last
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
1

You can simply use this.

let string = "file:///private/var/mobile/Containers/Data/Application/1C02189F-2426-4420-A178-1CC420CBBD3A/tmp/com.test.Test-Inbox/2015-04-23_00002.jpg"
let all = string.components(separatedBy: ".")
let ext = all.last
print(ext)
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
vikram
  • 181
  • 2
  • 5
1

You can get it by

NSURL *stringFilePath = [NSURL URLWithString:@"file:///private/var/mobile/Containers/Data/Application/1C02189F-2426-4420-A178-1CC420CBBD3A/tmp/com.test.Test-Inbox/2015-04-23_00002.jpg"];

NSString *fileName = [stringFilePath lastPathComponent];

And get extension

NSLog(@"%@", fileName.pathExtension)

For Swift :

let stringFilePath:NSString = "file:///private/var/mobile/Containers/Data/Application/1C02189F-2426-4420-A178-1CC420CBBD3A/tmp/com.test.Test-Inbox/2015-04-23_00002.jpg"
let strFileExtenson = stringFilePath.pathExtension
print(strFileExtenson)
Govaadiyo
  • 5,644
  • 9
  • 43
  • 72
0

Try this :

let fileNameWithExtension = "file:///private/var/mobile/Containers/Data/Application/1C02189F-2426-4420-A178-1CC420CBBD3A/tmp/com.test.Test-Inbox/2015-04-23_00002.jpg"
let url = URL(string:fileNameWithExtension)
print(url?.pathExtension ?? "")
KKRocks
  • 8,222
  • 1
  • 18
  • 84