开发者

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??


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜