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.
You could use stringByDeletingLastPathComponent
twice:
NSString *pathStr = @"/a/b/c/d";
NSString *path = [[pathStr stringByDeletingLastPathComponent] stringByDeletingLastPathComponent];
NSLog(@"%@", path);
Returns /a/b
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With