开发者

Get filename and extension from filepath programmatically cocoa?

开发者 https://www.devze.com 2023-03-31 03:12 出处:网络
I know you can use NSBundle: NSString *filePath = [[NSBundle mainBundle] pathForResource:@\"foo\" ofType:@\"rtf\"];

I know you can use NSBundle:

 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"rtf"];

to get the filepath of a file, but how would I get the filename and extension (separately) from the filepath programmatically using NSBundle.

For example, I may have: /Users/theuser/Documents/yourdocument.txt

It is obvious that the file is 'yourdocument', and 开发者_如何学运维the extension 'txt'

But I need to get the filename and extension from the filepath to use as an NSString.

Your help is greatly appreciated!


There are methods on NSString that do this. Look at -[NSString pathExtension] and -[NSString lastPathComponent], both defined in NSPathUtilities.h.


to get the filename without extension, try out [NSString stringByDeletingPathExtension];


Try this, it works for me.

NSString *fileName = @"yourFileName.pdf";
NSString *ext = [fileName pathExtension];


i hope this will help you....

Str = [NSString stringWithFormat:@"%@",[openPanel URL]];

  [txtBeowsFilePath setStringValue:Str];

  Browesfilename=[Str lastPathComponent];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号