Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to parse NSString by removing 2 folders in path in Objective-C

I need to parse NSString in Objective-C.. i.e if input path string is /a/b/c/d , i need to parse the path string to get the outout like /a/b/
How do i achieve it? input path string:/a/b/c/d expected output path string:/a/b/ Please help me out.

Thank You. Suse.

like image 968
suse Avatar asked Sep 19 '11 10:09

suse


2 Answers

You could use stringByDeletingLastPathComponent twice:

NSString *pathStr = @"/a/b/c/d";
NSString *path = [[pathStr stringByDeletingLastPathComponent] stringByDeletingLastPathComponent];
NSLog(@"%@", path);

Returns /a/b.

like image 61
Alex Avatar answered Nov 09 '22 23:11

Alex


How about:

NSString *path = @"/a/b/c/d";
NSArray *components = [path pathComponents]

NSLog(@"%@", [components objectAtIndex: 1]); // <- output a
NSLog(@"%@", [components objectAtIndex: 2]); // <- output b
NSLog(@"%@", [components lastObject]); // <- output d
like image 30
evets Avatar answered Nov 10 '22 01:11

evets