I need to parse NSString in Objective-C.. i.e if input path string is /a/b/c/d , i need to par开发者_运维百科se 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
NSString *path = @"/a/b/c/d";
int howManyFoldersNeedsToBeDeleded = 2;
for (int i = 1; i <= howManyFoldersNeedsToBeDeleded; i++) {
path = [path stringByDeletingLastPathComponent];
}
NSLog(@"output : %@ \n\n",path);
精彩评论