2011-09-19 2 views

답변

16

당신은 두 번 stringByDeletingLastPathComponent을 사용할 수

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

반환 /a/b합니다.

+0

이 – suse

+0

가 .. 너무 많은 Upvoted 감사합니다 정말 감사합니다 :) – Karun

0
NSString *path = @"https://stackoverflow.com/a/b/c/d"; 


int howManyFoldersNeedsToBeDeleded = 2; 

for (int i = 1; i <= howManyFoldersNeedsToBeDeleded; i++) { 


    path = [path stringByDeletingLastPathComponent]; 




} 



NSLog(@"output : %@ \n\n",path); 
1

에 대해 어떻게 :

NSString *path = @"https://stackoverflow.com/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 
관련 문제