I need to parse an NSString in Objective-C .. ie if the input path string is / a / b / c / d, I need to parse the path string to get outout like / a / b /How do I achieve this? input path line: / a / b / c / d expected output path line: / a / b / Please help me.
Thanks. Suse.
You can use stringByDeletingLastPathComponent twice:
stringByDeletingLastPathComponent
NSString *pathStr = @"/a/b/c/d"; NSString *path = [[pathStr stringByDeletingLastPathComponent] stringByDeletingLastPathComponent]; NSLog(@"%@", path);
Returns /a/b .
/a/b
What 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);
Source: https://habr.com/ru/post/897601/More articles:Seamless recording on iPad - iosCursors + Pagination & SEO - seoRenaming file names containing spaces - pythonHow to avoid pagination on a website to have a flat architecture? - seoiOS SDK that allows the user to draw on the screen and save as an image? - iosRSA blind signature using .NET cryptographic API? - .netScala multi-partition card type mismatch; Found (A, B) => Requires Boolean (A, B) => Boolean? - typesHorizontalScrollView, auto-scroll to the end with animation - androidremove specific attributes from HTML tags - pythonHow to make a program always run the first iteration of a while loop? - javaAll Articles