1

我有一个包含对象的数组,可以说{我的萨克斯管,到处走}。其次,我有一个NSString:“把我的萨克斯管带到任何地方”,那么我怎样才能将数组对象与字符串匹配并从中获取索引NSString。我需要NSString通过与数组对象进行比较来获取子字符串的索引。这样我就可以正确地对数组进行排序。到目前为止我已经完成了

NSRange range = [feedBackAnswer rangeOfString:@"everywhere"];
        if ( range.length > 0 ) 
        {
            NSLog(@"range.location..%d",range.location);
        } 
        else 
        {
            NSLog(@"...%d",-1);
        }

但是我找不到子字符串的索引。

4

3 回答 3

1
    NSMutableArray * data=[[NSMutableArray alloc]initWithObjects:@"my saxophone",@"take",@"everwhere", nil];
        NSString * mat=@"take my saxophone everywhere";
        for (int i=0; i<[data count]; i++) {
            NSRange range = [mat rangeOfString:[data objectAtIndex:i]];
            if (range.length > 0)
                NSLog(@"Range is: %@", NSStringFromRange(range));
            else
                NSLog(@"Failed"); 
        }

[data release];

& 检查数组中的拼写(everwhere

于 2012-08-13T12:35:58.943 回答
0

字符串中子字符串的范围是...

   0: U

   1: n

   2: i

   3: v

   4: e

   5: r

   6: s

   7: i

   8: t

   9: y
于 2012-08-13T12:28:21.163 回答
0

试试这个代码

NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"my",@"saxophone",@"take",@"everywhere", nil];

NSString *str1 = @"take my saxophone everywhere";

NSArray *array = [str1 componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@" objects : %@",array);

for (int i=0; i<[array count]; i++) {
    for (int j=0; j<[array count]; j++) {
    if ([[array objectAtIndex:i] isEqualToString:[array1 objectAtIndex:j]]) {
        NSLog(@"String :%@ no : %d ",[array objectAtIndex:i],j);
        }
    }
}
于 2012-08-13T12:52:22.637 回答