0

我有一个NSMutableArray里面的物品。我想将每个项目与一个字符串进行比较。如果字符串相同,则数组的下一项应存储在另一个数组中。

NSString *string2 = [NSString stringWithFormat:@"Period:"];

// Filtern nach Periode 
NSMutableArray *Eventarray =[NSMutableArray array];


for(int i=0;i<[lines count]; i++)
{

    NSMutableString *string1 = [NSMutableString stringWithFormat: @"%@",[[lines objectAtIndex:i]description]];
    //  NSLog(@"%@",string1);


    int index = [[lines objectAtIndex:i] indexOfObject:@"Period:"];
     NSLog(@"%@",index);

    if ([[lines objectAtIndex:i] isEqualToString:@"Period:"]) 
    {
        //strings are same

        NSLog(@"ii");

           NSLog(@"ii");
        int e=i+1;
        NSMutableString *Periode = [NSMutableString stringWithFormat: @"%@",[lines objectAtIndex:e]];

        [Eventarray addObject:Periode];
    }

    [string1 deleteCharactersInRange:NSMakeRange([string1 length]-1, 1)];

}

for(int i=0;i<[Eventarray count]; i++)
{
      NSLog(@"Eventarray: %@", [Eventarray objectAtIndex:i]);

}

数组如下所示:

2013-08-12 13:31:35.375 xxxx[3809:207] PublishedRoster
2013-08-12 13:31:35.376 xxxx[3809:207] Period:
2013-08-12 13:31:35.377 xxxx[3809:207] 25Jul2013-08Sep2013

我尝试了一切,但我不知道出了什么问题。我错过了什么?

4

1 回答 1

0

尝试使用NSRange

如果要搜索子字符串

NSString *homebrew = @"Rajneesh071";
NSRange range = [homebrew rangeOfString:@"Raj"];

// Did we find the string "Raj" ?
if (range.length > 0)
  NSLog(@"Range is: %@", NSStringFromRange(range));

在您的代码中,您可以这样做。

for(int i=0;i<[lines count]; i++)
{
    NSRange stringRange = [[lines objectAtIndex:i] rangeOfString:@"Period:" options:NSCaseInsensitiveSearch];

    if(stringRange.location != NSNotFound)
    {
        [Eventarray addObject:Periode];
    }
}
于 2013-08-12T11:46:22.593 回答