Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get text after certain point using NSString

I am getting a url of a file when a user opens one from an NSOpenPanel for example like so:

/Users/Name/Documents/MyFile.png

So I just want this bit:

MyFile.png

However the user could have a file name of any length so how can I say, only get the string after the last forward slash (/)? I just want to get the file name.

like image 504
Josh Kahane Avatar asked Feb 09 '26 15:02

Josh Kahane


1 Answers

NSString *fileName = [someStringContainingAPath lastPathComponent];

More general advice: spend a little time reading through the reference pages for NSString and NSString(UIStringDrawing). There are a lot of useful methods in there that you might not otherwise know to look for. In addition to -lastPathComponent demonstrated above, there's -pathComponents, -componentsSeparatedByString:, and many other handy tools.

like image 91
Caleb Avatar answered Feb 12 '26 14:02

Caleb