如何将数据从字符串存储到带分隔符的数组中

时间:2011-06-02 06:54:52

标签: iphone

  

可能重复:
  separate the ids and store in two different array

String response=@" hr 123,124,125,126,127,128 hr st 234,235,236,237 st";

从小时到小时我需要存储在一个数组中。

从st到st我需要存储在另一个数组中。

我的服务提供商正在播放响应.....

请帮帮我。

1 个答案:

答案 0 :(得分:1)

你去了:

    NSMutableString *response = [NSMutableString stringWithString: @" hr 123,124,125,126,127,128 hr st 234,235,236,237 st"];

    [response replaceOccurrencesOfString:@" " 
                              withString:@"" 
                                 options:NSCaseInsensitiveSearch
                                   range:NSMakeRange(0, [response length])];




    NSRange firstAppearanceOfHr = [response rangeOfString:@"hr"];

    [response replaceCharactersInRange:firstAppearanceOfHr withString:@""];



    NSRange secondAppearanceOfHr = [response rangeOfString:@"hr"];

    NSString *hrString = [response substringWithRange:NSMakeRange(0, secondAppearanceOfHr.location)];

    NSArray *hrArray = [hrString componentsSeparatedByString:@","];

    NSLog(@"HrArray:%@",[hrArray description]);

    NSRange firstAppearaceOfSt = [response rangeOfString:@"st"];

    NSInteger startSt = firstAppearaceOfSt.location+firstAppearaceOfSt.length;
    NSString *stString  = [response substringWithRange:NSMakeRange(startSt, [response length] - startSt - 2)];

    NSArray *stArray = [stString componentsSeparatedByString:@","];

    NSLog(@"StArray:%@",[stArray description]);