Reputation: 1475
I need to split NSString
to array by specific word.
I've tried to use [componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]]
But the split is performed by a single character, and I need a few chars.
Example:
NSString @"Hello great world";
Split key == @" great ";
result:
array[0] == @"Hello";
array[1] == @"world";
Upvotes: 0
Views: 219
Reputation: 2999
Try
NSString *str = @"Hello great world";
//you can use the bellow line to remove space
//str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
// split key = @"great"
NSArray *arr = [str componentsSeparatedByString:@"great"];
Upvotes: 1
Reputation: 1360
The easiest way is the following:
NSString *string = @"Hello Great World";
NSArray *stringArray = [string componentsSeparatedByString: @" "];
This can help you.
Upvotes: 0
Reputation: 2950
Code:
NSString *string = @"Hello great world";
NSArray *stringArray = [string componentsSeparatedByString: @" great "];
NSLog(@"Array 0: %@" [stringArray objectAtIndex:0]);
NSLog(@"Array 1: %@" [stringArray objectAtIndex:1]);
Upvotes: 0