R. Dewi
R. Dewi

Reputation: 4271

how to delete a substring and get another part from that string

I have some string that have a type like this

NSString *string1 = @"/Users/mine/Library/Application Support/iPhone Simulator/3.2/Applications/02221798-1B7A-46B4-928F-F5BE37E177B5/Documents/Project/Project 1/Folder 1/2.php";

if i just want to get the "02221798-1B7A-46B4-928F-F5BE37E177B5"

i have implement a code like this :

NSString *subString1;
int count = [string1 length];
NSLog(@"count : %d", count);
subString1 = [string1 substringFromIndex:121];

but it left "02221798-1B7A-46B4-928F-F5BE37E177B5/Documents/Project/Project 1/Folder 1/2.php"

how can i do to fix it??

Upvotes: 0

Views: 876

Answers (2)

Costique
Costique

Reputation: 23722

To get the name of your app's parent directory you should try this instead:

NSString *appUUID = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] lastPathComponent];

This will work both on a device and Simulator.

If you need to get the full path to the Documents folder inside your app sandbox:

NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

If you need to append a path component to a path string:

NSString *projectPath = [documentsPath stringByAppendingPathComponent: @"Project"];

Please, take a look at NSString documentation, section "Tasks", "Working with Paths". It is a must-read.

Upvotes: 1

Kapil Choubisa
Kapil Choubisa

Reputation: 5232

you can use stringWithRange method of NSString. subString1 = [string1 stringWithRange:NSMakeRange(startPos,yourLengthToSelect)];

or you can use componentSeparetedByString and break string using @"/". it will return array of string. get your desired string using array index.

Upvotes: 0

Related Questions