views:

72

answers:

2

I have a lot of strings looking like this:

@"/News/some news text/"
@"/News/some other news text/"
@"/About/Some about text/"
@"/Abcdefg/Some abcdefg text/some more abcdefg text"

How do I cut out the first part of the strings, so that I end up with the following strings?:

@"/News/"
@"/News/"
@"/About/"
@"/Abcdefg/"

tia

+2  A: 

Use componentsSeparatedByString: to break the string up:

NSArray *components=[string componentsSeparatedByString:@"/"];
if ([components count]>=2) {
     // Text after the first slash is second item in the array
    return [NSString stringWithFormat:@"/%@/",[components objectAtIndex:1]];
} else {
    return nil; // Up to you what happens in this situation
}
grahamparks
Thank you SO much!!
Malene
@Malene: If this answer works for you, then please give grahamparks proper credit by accepting the answer.
Philip Regan
sorry - didn't know about that . . .-answer accepted;-)
Malene
A: 

If these are pathnames, you may want to look into the path-related methods of NSString, such as pathComponents and pathByDeletingLastPathComponent.

While it's pretty unlikely that the path separator is ever going to change, it's nonetheless a good habit to not rely on such things and use dedicated path-manipulation methods in preference to assuming that the path separator will be a certain character.

Peter Hosey