开发者

Get text after certain point using NSString

开发者 https://www.devze.com 2023-02-16 02:10 出处:网络
I am gett开发者_JAVA百科ing a url of a file when a user opens one from an NSOpenPanel for example like so:

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.

0

精彩评论

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