我有一个包含对象的数组,请说{my saxophone,take,everywhere}。其次我有NSString
:'把萨克斯管带到各处'然后我怎样才能将数组的对象与字符串匹配并从NSString
获得索引。通过与数组对象进行比较,我需要来自NSString
的子字符串索引。这样我就可以正确地对数组进行排序。到目前为止我已经完成了
NSRange range = [feedBackAnswer rangeOfString:@"everywhere"];
if ( range.length > 0 )
{
NSLog(@"range.location..%d",range.location);
}
else
{
NSLog(@"...%d",-1);
}
但是我找不到子串的索引。
答案 0 :(得分: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];
&安培;检查数组中的拼写(无处不在)
答案 1 :(得分:0)
字符串中的子字符串范围是......
0: U
1: n
2: i
3: v
4: e
5: r
6: s
7: i
8: t
9: y
答案 2 :(得分: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);
}
}
}