I added a category on NSString
to split on the first occurrence of a given string. It may not be ideal to return the results in an array, but otherwise it seems fine. It just uses the NSString
method rangeOfString:
, which takes an NSString
(B) and returns an NSRange
showing where that string(B) is located.
@interface NSString (Split)
- (NSArray *)stringsBySplittingOnString:(NSString *)splitString;
@end
@implementation NSString (Split)
- (NSArray *)stringsBySplittingOnString:(NSString *)splitString
{
NSRange range = [self rangeOfString:splitString];
if (range.location == NSNotFound) {
return nil;
} else {
NSLog(@"%li",range.location);
NSLog(@"%li",range.length);
NSString *string1 = [self substringToIndex:range.location];
NSString *string2 = [self substringFromIndex:range.location+range.length];
NSLog(@"String1 = %@",string1);
NSLog(@"String2 = %@",string2);
return @[string1, string2];
}
}
@end