-3

I have a string /uploads/images/mypic_123.png now i want to get only image name like mypic_123.png.Please tell me how to do it? I am using objective-c.

2
  • [string lastPathComponent]. Aug 19, 2015 at 11:51
  • 1
    Your strings seem to be paths. Maybe you can reflect that in the question and its title.
    – Felix
    Aug 19, 2015 at 12:09

3 Answers 3

3

NSString has a method named lastPathComponent that you can use

2

you can use this way:-

 NSURL *url;
 NSString *substring=[url lastPathComponent];
 NSLog(@"Your image name is :%@",substring);
1

You may also use the below code

NSString *str = @"/uploads/images/mypic_123.png";
NSArray *parts = [str componentsSeparatedByString:@"/"];
NSString *filename = [parts lastObject];
1
  • I don't think this works with paths that end with a /.
    – user7085621
    Jul 12, 2019 at 14:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.