开发者

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 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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜