Subin Kurian
Subin Kurian

Reputation: 293

Find out the extension part of URL saved in NSString

How to find out and save the extension of fields that i am going to save in photo album. i want know it which format. example:

http://www.example.com/myvideo.mp4, http://www.example.com/mypicture.png

I need is like .mp4 and .png.If possible give me example code.

Upvotes: 13

Views: 10943

Answers (4)

pableiros
pableiros

Reputation: 16032

For Swift:

let stringUrl = " http://www.example.com/mypicture.png"
let url = URL(string: stringUrl)
let path = url?.path
let fileExtension = url?.pathExtension

Upvotes: 0

yogs
yogs

Reputation: 788

NSURL also have pathExtension (Available in iOS 4.0 and later.)

NSString *extension = [[NSURL URLWithString: @"http://sample.example.com/path/hellowwrod.ext"] pathExtension];

Upvotes: 9

bitmapdata.com
bitmapdata.com

Reputation: 9600

refer a following code.

NSString *path = @"http://www.mysite.com/myvideo.mp4";
NSString *lastPath = [path lastPathComponent];
NSString *fileExtension = [lastPath pathExtension]; // [path pathExtension];
NSLog(@"%@", lastPath); //myvideo.mp4
NSLog(@"%@", fileExtension); // mp4

Upvotes: 6

Jonathan Grynspan
Jonathan Grynspan

Reputation: 43472

Have you looked at the documentation for NSString? You just need to send -pathExtension to your string.

If you're dealing with a string containing a URL, you should first convert it to an NSURL, then extract the path:

NSString *stringURL = @"http://...";
NSURL *url = [NSURL URLWithString:stringURL];
NSString *path = [url path];
NSString *extension = [path pathExtension];

Upvotes: 20

Related Questions