简体   繁体   中英

How to get path extension in iOS

If I have a file name like this how do I get the path extension?

http://example.com/image.png?key=value

If I use -pathExtension for NSString it returns jpg?key=value .

I want to get the true extension from the path so just jpg .

Try your code in swift. It works fine.

let url = URL(string: "http://example.com/image.png?key=value")
print(url?.pathExtension) //Optional("png")

Try this:

NSURL *url = [NSURL URLWithString:@"http://example.com/image.png?key=value"];
NSString *ext = [[url lastPathComponent] pathExtension];
NSLog(@"%@", ext) ;  // png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM