如何切出部分NSString?

Mal*_*ene 0 iphone cocoa objective-c nsstring

@"/News/some news text/" 
@"/News/some other news text/" 
@"/About/Some about text/" 
@"/Abcdefg/Some abcdefg text/some more abcdefg text"
Run Code Online (Sandbox Code Playgroud)

如何剪切字符串的第一部分,以便最终得到以下字符串?

@"/News/"
@"/News/"
@"/About/"
@"/Abcdefg/"
Run Code Online (Sandbox Code Playgroud)

gra*_*rks 8

使用componentsSeparatedByString:打破字符串,最多:

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
}
Run Code Online (Sandbox Code Playgroud)