0

如何使用rangeOfString大写和小写字母搜索字符串?

这就是我到目前为止所拥有的;

for (NSString* key in words) {

if ([searchfield.text rangeOfString:key].location != NSNotFound) 
{

       NSString *textresult = [words objectForKey:key];
       NSLog(@"Answer: %@", textresult);
       result.text = textresult;

       result.tag = 1;

}

谢谢

4

3 回答 3

2

key只需以大写或小写形式搜索相应的:

[searchField.text rangeOfString:[key lowercaseString]].location != NSNotFound

[searchField.text rangeOfString:[key uppercaseString]].location != NSNotFound

如果您不关心用户使用的是大写还是小写,只需使用以下NSCaseInsensitiveSearch选项:

[searchfield.text rangeOfString:key options: NSCaseInsensitiveSearch].location 
   != NSNotFound
于 2012-04-17T20:36:07.600 回答
0

我会使用 NSRange 和 [NSString lowercaseString] 创建搜索字符串和要搜索的单词的小写版本。

然后使用小写搜索字符串的 rangeOfString 方法生成一个 NSRange,然后我会使用“successRange”来输出相应的原始文本块。

这是我为您准备的一个工作示例:

NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", @"key3", @"key4", @"key5",@"key6", nil];

NSArray *objects = [NSArray arrayWithObjects:@"How", @"are", @"you", @"doing", @"?",@"How are you doing?", nil];


NSDictionary * words = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NSString * searchString = @"are";
NSString * lowercaseSearchString = [searchString lowercaseString];

for(NSString * key  in words){

    NSString *  text = [words objectForKey:key];
    NSString * lowercaseText = [text lowercaseString];

    NSRange successRange = NSRangeFromString(@"");

    NSRange lowercaseRange = [lowercaseText rangeOfString:lowercaseSearchString];

    if(lowercaseRange.location!=NSNotFound){
        successRange = lowercaseRange;
    }

    NSLog(@"key: %@, text: %@", key,text);
    if(successRange.length >0){
        NSLog(@"    text subStringWithRange: %@",  [text substringWithRange:successRange]);         
    }
}
于 2012-04-17T22:32:42.070 回答
0
  -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
 { 
      flavourarray = [[NSMutableArray alloc]init];
      imagearray = [[NSMutableArray alloc]init];
      categoryarray = [[NSMutableArray alloc]init];
  [filteredListContent removeAllObjects];   // clear the filtered array first
 searchText=[searchText uppercaseString];

    NSString *cellTitle;

BOOL flag1=NO;
searchText=[searchText lowercaseString];
int i1=0;
j=0;
for (cellTitle in listContent)
{
NSComparisonResult result = [cellTitle compare:searchText     
    options:NSCaseInsensitiveSearch range:NSMakeRange(0, [searchText length])];
    if (result == NSOrderedSame)
    {

        if(flag1==NO)
        {
            filtered_flag=i1;
            flag1=YES;
        }
        [filteredListContent addObject:cellTitle];
        NSString *searchflavour = [[categoryarray_ipad objectAtIndex:j]objectForKey:@"flavour"];
        NSString *searchcategory = [[categoryarray_ipad  
         objectAtIndex:j]objectForKey:@"category"];
        NSString *searchimage = [[categoryarray_ipad objectAtIndex:j]objectForKey:@"image"];

        if (searchText.length !=0) {
        [categoryarray addObject:searchcategory];
        }
        [flavourarray addObject:searchflavour];
        [imagearray addObject:searchimage];
    }
    j++;
    i1++;
}
NSLog(@"filtered_flag:%d",filtered_flag);
NSLog(@"filtered:%@",filteredListContent);
searchresult =TRUE;

[itemstable_ipad reloadData];

}

于 2012-04-18T05:17:54.280 回答