按特定单词将NSString拆分为数组

时间:2015-10-19 07:31:29

标签: ios objective-c arrays nsstring

我需要按特定字词将NSString拆分为数组。

我尝试使用[componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]]

但是分裂是由一个角色完成的,我需要几个字符。

示例:

NSString @"Hello great world";

拆分键== @“伟大”;

结果:

array[0] == @"Hello";
array[1] == @"world";

3 个答案:

答案 0 :(得分:1)

尝试

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"];

答案 1 :(得分:0)

代码:

NSString *string = @"Hello great world";
NSArray *stringArray = [string componentsSeparatedByString: @" great "];

NSLog(@"Array 0: %@" [stringArray objectAtIndex:0]);
NSLog(@"Array 1: %@" [stringArray objectAtIndex:1]);

答案 2 :(得分:0)

最简单的方法如下:

NSString * string = @“Hello Great World”;

NSArray * stringArray = [string componentsSeparatedByString:@“”];

这可以帮到你。