Chatar Veer Suthar
Chatar Veer Suthar

Reputation: 15639

How to take NSString after specific character which occurs multiple time in NSString?

I have a complete URL, say

http://www.mywebsite.com//Folder /Detals /Final Image /La Image Logo.jpg

Here in this NSString, I only want to fetch

La Image Logo.jpg

How can I do so?

Thanks

Upvotes: 2

Views: 87

Answers (3)

nicael
nicael

Reputation: 18995

You can use NSString *string=[[yourString componentsSeparatedByString:@"/"] lastObject]

Upvotes: 1

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81868

If it's about the filename in a URL you should definitely create an NSURL object and query that object for the desired information:

NSURL *url = [NSURL URLWithString:@"http://www.mywebsite.com//Folder /Detals /Final Image /La Image Logo.jpg"];
NSString *filename = [url lastPathComponent];

NSURL is good at parsing URL strings. This way you're sure that you don't get any fragment part ("#anchor") or parameters ("?q=foo&p=bar") in your filename.

Upvotes: 2

slecorne
slecorne

Reputation: 1718

[string lastPathComponent] should do the trick

Upvotes: 3

Related Questions