如何使字符串中包含的单词数组?

时间:2010-09-22 09:32:01

标签: iphone

我有一个字符串

gpbusd buy update HIT 40 PIPS HIT 110 PIPS gpbusd buy BREAK EVEN update HIT 100+ gpbusd buy 1.5500/25 1.5455 new 40 100+ gpbusd buy update CLOSE 0 TO 10 PIPS N gpbusd buy 1.5335/50 1.5320 new 40 80+ gpbusd buy update 15-20 PIPS CLOSE KEEP OPEN gpbusd buy 1.5530/50 1.5505 update HIT 80 KEEP OPEN gpbusd buy 1.5530/50 1.5465 new 40 80 100+ gbpjpy sell 131.05/.130.75 132.15 new 60 100 keep open eurusd sell 1.2840/20 1.2870 STOP update

我想在字符串的空格之间包含单词的数组?我该怎么做?

2 个答案:

答案 0 :(得分:3)

您可以尝试使用

NSArray *words = [myString componentsSeparatedByString:@" "];

More here

答案 1 :(得分:0)

这是您正在寻找的答案......这将检查多个空格

用你的字符串替换str(我使用str作为例子)

NSMutableArray * arr;

NSString *str=@"mango        apple    banana grapes    pineapple";
NSRange match;  

arr=[[NSMutableArray alloc]init];


while(str.length>0)
{
match = [str rangeOfString: @" "];
    if(match.location>=str.length)
    {
        [arr addObject:str];
        break;
    }
NSString *str2=[str substringToIndex:match.location];
str=[str substringFromIndex:match.location+1];
    if(str2.length==0)
    {
        continue;   
    }
[arr addObject:str2];
}
NSLog(@"array is %@",arr);    //here is your array

这在我的应用程序中工作正常。希望它也能帮到你