我需要按特定字词将NSString
拆分为数组。
我尝试使用[componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]]
但是分裂是由一个角色完成的,我需要几个字符。
示例:
NSString @"Hello great world";
拆分键== @“伟大”;
结果:
array[0] == @"Hello";
array[1] == @"world";
答案 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:@“”];
这可以帮到你。