Get text after certain point using NSString
I am gett开发者_JAVA百科ing 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.
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.
精彩评论