是否有比这更好(更简洁/有效/更优雅)的方法将包含斜杠的字符串分成两部分:一个在前一个,另一个在最后一个斜杠之后。
NSRange range = [s rangeOfString: @"/" options: NSBackwardsSearch];
NSAssert(range.location != NSNotFound, nil);
NSString *s1 = [s substringToIndex: range.location];
NSString *s2 = [s substringFromIndex: range.location + 1];
答案 0 :(得分:2)
例如
NSArray *components = [s componentsSeparatedByString:@"/"];
NSString *s1 = ((![components count])? nil :[components objectAtIndex:0] );
NSString *s2 = ((![components count]>0)? nil :[components objectAtIndex:1] );