我有一个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
我尝试了一切,但我不知道什么是错的。我错过了什么?
答案 0 :(得分: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];
}
}