将 NSString 分成两部分(最后一个斜线之前和之后)

Splitting NSString into two parts (before and after last slash)

有没有比这更好(更多concise/efficient/elegant)的方法来将包含斜杠的字符串分成两部分:一个在最后一个斜杠之前,另一个在最后一个斜杠之后。

NSRange range = [s rangeOfString: @"/" options: NSBackwardsSearch];
NSAssert(range.location != NSNotFound, nil);

NSString *s1 = [s substringToIndex: range.location];
NSString *s2 = [s substringFromIndex: range.location + 1];

你可以使用 - componentsSeparatedByString:

例如

NSArray *components = [s componentsSeparatedByString:@"/"];

NSString *s1 = ((![components count])? nil :[components objectAtIndex:0] );
NSString *s2 = ((![components count]>0)? nil :[components objectAtIndex:1] );