2011-09-19 8 views
6

Necesito analizar NSString en Objective-C ... es decir, si la cadena de ruta de entrada es/a/b/c/d, necesito analizar la cadena de ruta para obtener el resultado como/a/b/
¿Cómo lo logro? cadena de ruta de entrada:/a/b/c/d cadena de ruta de salida esperada:/a/b/ Por favor, ayúdenme.cómo analizar NSString eliminando 2 carpetas en ruta en Objective-C

Gracias. Suse.

Respuesta

16

Usted podría utilizar stringByDeletingLastPathComponent dos veces:

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

devoluciones /a/b.

+0

Muchas gracias – suse

+0

Muchas gracias .. :) 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

¿Qué tal:

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 
Cuestiones relacionadas